I have some UILabels whose backgroundColor
are white or red, and opaque are all YES.
But when the Color Blended Layers
option is selected in Simulator, these UILabels are marked as red instead of green.
What else options can cause these?
My code is as followed:
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
self.senderNameLabel = [UILabel mt_labelWithFont:FONT(17) andColor:COLOR_NAV_TITLE];
self.senderNameLabel.backgroundColor = [UIColor whiteColor];
[self.contentView addSubview:self.senderNameLabel];
self.actionTextLabel = [UILabel mt_labelWithFont:FONT(15) andColor:COLOR_NAV_TITLE];
self.actionTextLabel.numberOfLines = 5;
self.actionTextLabel.backgroundColor = [UIColor whiteColor];
[self.contentView addSubview:self.actionTextLabel];
self.notifyDateLabel = [UILabel mt_labelWithFont:FONT(12) andColor:COLOR_NAV_TITLE];
self.notifyDateLabel.backgroundColor = [UIColor whiteColor];
[self.contentView addSubview:self.notifyDateLabel];
[self setLayoutConstraints];
}
return self;
}
- (void)setLayoutConstraints {
CGFloat offsetX = 70;
CGFloat offsetY = 15;
CGFloat maxWidth = SCALE_WITH_RATIO(180);
[self.senderNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.contentView).offset(offsetX);
make.top.equalTo(self.contentView).offset(offsetY);
make.width.mas_lessThanOrEqualTo(maxWidth);
}];
offsetY = 12.5;
maxWidth = SCALE_WITH_RATIO(180);
[self.actionTextLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.senderNameLabel.mas_bottom).offset(offsetY);
make.left.equalTo(self.senderNameLabel);
make.width.mas_lessThanOrEqualTo(maxWidth);
}];
offsetY = 10;
CGFloat bottomOffsetY = -15;
[self.notifyDateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.actionTextLabel.mas_bottom).offset(offsetY);
make.left.equalTo(self.senderNameLabel);
make.bottom.equalTo(self.contentView).offset(bottomOffsetY);
}];
}
- (void)prepareForReuse {
[super prepareForReuse];
self.senderNameLabel.text = nil;
self.actionTextLabel.text = nil;
self.notifyDateLabel.text = nil;
}
- (void)updateWithMessageModel:(MTUserMessageModel *)model {
self.senderNameLabel.text = model.senderName;
self.actionTextLabel.text = model.actionText;
self.notifyDateLabel.text = model.notifyDate;
}
The helper method for UILabel
generation is as follow:
+ (UILabel *)mt_labelWithFont:(UIFont *)font andColor:(UIColor *)color {
UILabel *label = [UILabel new];
label.font = font;
label.textColor = color;
return label;
}
The snapshot from simulator is as follow
Just find a solution:
titleLabel.clipsToBounds = YES;
titleLabel.backgroundColor = [UIColor whiteColor];
Then it become green again.
I found the link that says when displaying Chinese UILabel
will have an extra sublayer.
Say if you have a UILabel instance called titleLabel
. Setting text with Chinese or English, and check the sublayers using debug command:
po [[titleLabel layer] sublayers]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With