HJPortraitView.m 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. //
  2. // HJPortraitView.m
  3. // HappyJob
  4. //
  5. // Created by Bob on 2019/4/4.
  6. // Copyright © 2019 Bob. All rights reserved.
  7. //
  8. #import "HJPortraitView.h"
  9. @interface HJPortraitView ()
  10. @end
  11. @implementation HJPortraitView
  12. - (instancetype)initWithFrame:(CGRect)frame {
  13. if (self = [super initWithFrame:frame]) {
  14. self.backgroundColor = [UIColor whiteColor];
  15. self.layer.borderWidth = 1;
  16. self.layer.borderColor = [UIColor colorWithRed:240/255.0 green:240/255.0 blue:240/255.0 alpha:1.0].CGColor;
  17. [self addSubview:self.imageView];
  18. }
  19. return self;
  20. }
  21. - (void)setFrame:(CGRect)frame {
  22. [super setFrame:frame];
  23. CGFloat radius = MIN(frame.size.width, frame.size.height) / 2;
  24. self.layer.cornerRadius = radius;
  25. self.layer.masksToBounds = YES;
  26. CGFloat scale = 16; // 缩放系数
  27. CGFloat x = MIN(6, ceilf(frame.size.width / scale)) + self.layer.borderWidth; // 最大间距不能超过6
  28. CGFloat y = MIN(6, ceilf(frame.size.height / scale)) + self.layer.borderWidth;
  29. CGFloat w = frame.size.width - x * 2;
  30. CGFloat h = frame.size.height - y * 2;
  31. self.imageView.frame = CGRectMake(x, y, w, h);
  32. self.imageView.layer.cornerRadius = MIN(w, h) / 2;
  33. self.imageView.layer.masksToBounds = YES;
  34. }
  35. #pragma mark - getters and setters
  36. - (void)setImage:(UIImage *)image {
  37. _image = image;
  38. self.imageView.image = image;
  39. }
  40. - (UIImageView *)imageView {
  41. if (_imageView == nil) {
  42. _imageView = [[UIImageView alloc] init];
  43. }
  44. return _imageView;
  45. }
  46. @end