我有一些UILabel,其backgroundColor
是白色或红色,而不透明的颜色都是YES。
但是,当在Simulator中选择Color Blended Layers
选项时,这些UILabel将标记为红色而不是绿色。
还有哪些选择可以导致这些?
我的代码如下:
- (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;
}
生成
UILabel
的辅助方法如下:+ (UILabel *)mt_labelWithFont:(UIFont *)font andColor:(UIColor *)color {
UILabel *label = [UILabel new];
label.font = font;
label.textColor = color;
return label;
}
来自模拟器的快照如下
最佳答案
只是找到一个解决方案:
titleLabel.clipsToBounds = YES;
titleLabel.backgroundColor = [UIColor whiteColor];
然后它再次变成绿色。
我发现link表示在显示中文
UILabel
时将有一个额外的子层。假设您有一个名为
titleLabel
的UILabel实例。用中文或英文设置文本,并使用debug命令检查子层:po [[titleLabel layer] sublayers]
关于uitableview - 选择 `Color Blended Layers`时,UILabel被标记为红色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34895641/