| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859 |
- //
- // HJPortraitView.m
- // HappyJob
- //
- // Created by Bob on 2019/4/4.
- // Copyright © 2019 Bob. All rights reserved.
- //
- #import "HJPortraitView.h"
- @interface HJPortraitView ()
- @end
- @implementation HJPortraitView
- - (instancetype)initWithFrame:(CGRect)frame {
- if (self = [super initWithFrame:frame]) {
- self.backgroundColor = [UIColor whiteColor];
- self.layer.borderWidth = 1;
- self.layer.borderColor = [UIColor colorWithRed:240/255.0 green:240/255.0 blue:240/255.0 alpha:1.0].CGColor;
-
- [self addSubview:self.imageView];
- }
- return self;
- }
- - (void)setFrame:(CGRect)frame {
- [super setFrame:frame];
-
- CGFloat radius = MIN(frame.size.width, frame.size.height) / 2;
- self.layer.cornerRadius = radius;
- self.layer.masksToBounds = YES;
-
- CGFloat scale = 16; // 缩放系数
- CGFloat x = MIN(6, ceilf(frame.size.width / scale)) + self.layer.borderWidth; // 最大间距不能超过6
- CGFloat y = MIN(6, ceilf(frame.size.height / scale)) + self.layer.borderWidth;
- CGFloat w = frame.size.width - x * 2;
- CGFloat h = frame.size.height - y * 2;
- self.imageView.frame = CGRectMake(x, y, w, h);
- self.imageView.layer.cornerRadius = MIN(w, h) / 2;
- self.imageView.layer.masksToBounds = YES;
- }
- #pragma mark - getters and setters
- - (void)setImage:(UIImage *)image {
- _image = image;
- self.imageView.image = image;
- }
- - (UIImageView *)imageView {
- if (_imageView == nil) {
- _imageView = [[UIImageView alloc] init];
- }
- return _imageView;
- }
- @end
|