@interface BSTTextPromtView ()
/** <#statements#> */
@property (nonatomic, strong) UIView *lineView;
/** <#statements#> */
@property (nonatomic, strong) UIImageView *iconImageView;
/** <#statements#> */
@property (nonatomic, strong) UILabel *textLabel;
@end
@implementation BSTTextPromtView
- (instancetype)initWithFrame:(CGRect)frame
{
NSLog(@"%s frame:%@",__func__,NSStringFromCGRect(self.frame));
self = [super initWithFrame:frame];
if (self) {
[self initUI];
}
return self;
}
- (instancetype)initWithCoder:(NSCoder *)coder
{
self = [super initWithCoder:coder];
NSLog(@"%s frame:%@",__func__,NSStringFromCGRect(self.frame));
if (self) {
[self initUI];
}
return self;
}
- (void)initUI {
NSLog(@"%s frame:%@",__func__,NSStringFromCGRect(self.frame));
self.backgroundColor = [UIColor whiteColor];
[self addSubview:self.lineView];
[self addSubview:self.iconImageView];
[self addSubview:self.textLabel];
}
- (void)layoutSubviews {
NSLog(@"%s frame:%@",__func__,NSStringFromCGRect(self.frame));
[super layoutSubviews];
self.lineView.frame = CGRectMake(0, 0, CGRectGetWidth(self.frame), 0.5);
self.iconImageView.frame = CGRectMake(32, 12, 14, 14);
UIFont *textFont = [UIFont systemFontOfSize:11.0];
CGFloat textHeight = [self.textLabel.text boundingRectWithSize:CGSizeMake(CGRectGetWidth(self.frame)- 50-32, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:textFont} context:nil].size.height;
self.textLabel.frame = CGRectMake(50, 12, CGRectGetWidth(self.frame)- 50-32, textHeight);
}
- (void)drawRect:(CGRect)rect {
[super drawRect:rect];
NSLog(@"%s frame:%@",__func__,NSStringFromCGRect(self.frame));
}
- (void)updateConstraints {
NSLog(@"%s frame:%@",__func__,NSStringFromCGRect(self.frame));
// 苹果官方说法:Call `[super update<wbr>Constraints]` as the final step in your implementation.
[super updateConstraints];
}
- (void)setImage:(UIImage *)image {
_image = image;
self.iconImageView.image = image;
}
- (void)setText:(NSString *)text {
_text = text;
self.textLabel.text = text;
}
- (UIView *)lineView {
if (!_lineView) {
_lineView = [[UIView alloc] initWithFrame:CGRectZero];
_lineView.backgroundColor = [UIColor colorWithRed:4/255.0 green:4/255.0 blue:22/255.0 alpha:0.15];
}
return _lineView;
}
- (UIImageView *)iconImageView {
if (!_iconImageView) {
_iconImageView = [[UIImageView alloc] initWithFrame:CGRectZero];
_iconImageView.image = [UIImage imageNamed:@"icon_properties"];
}
return _iconImageView;
}
- (UILabel *)textLabel {
if (!_textLabel) {
NSString *text = @"该内容来自第三方网站分享,非公司官方观点,";
UIFont *textFont = [UIFont systemFontOfSize:11.0];
_textLabel = [[UILabel alloc] initWithFrame:CGRectZero];
_textLabel.text = text;
_textLabel.numberOfLines = 0;
_textLabel.textColor = [UIColor colorWithRed:164/255.0 green:160/255.0 blue:168/255.0 alpha:1.0];
_textLabel.font = textFont;
}
return _textLabel;
}
@end
从xib加载打印如下:
2020-11-05 11:30:58.038243+0800 aa[81499:784826] -[BSTTextPromtView initWithCoder:] frame:{{57, 204}, {309, 65}}
2020-11-05 11:30:58.038399+0800 aa[81499:784826] -[BSTTextPromtView initUI] frame:{{57, 204}, {309, 65}}
2020-11-05 11:30:58.060203+0800 aa[81499:784826] -[BSTTextPromtView updateConstraints] frame:{{57, 204}, {309, 65}}
2020-11-05 11:30:58.060876+0800 aa[81499:784826] -[BSTTextPromtView layoutSubviews] frame:{{57, 204}, {309, 65}}
2020-11-05 11:30:58.064591+0800 aa[81499:784826] -[BSTTextPromtView drawRect:] frame:{{57, 204}, {309, 65}}
可以看到updateConstraints
> layoutSubviews
>drawRect
其中显示调用方法为
// updateConstraints:
[self setNeedsUpdateConstraints];
[self updateConstraintsIfNeeded];
// layoutSubviews:
[self setNeedsLayout];
[self layoutIfNeeded];
// drawRect: If you subclass UIView directly, your implementation of this method does not need to call super. However, if you are subclassing a different view class, you should call super at some point in your implementation.
[self setNeedsDisplay];
网友评论