Uitableview 选择“颜色混合层”时,UILabel标记为红色
我有一些UILabel,它们的Uitableview 选择“颜色混合层”时,UILabel标记为红色,uitableview,uilabel,ios-simulator,Uitableview,Uilabel,Ios Simulator,我有一些UILabel,它们的背景色是白色或红色,不透明的都是肯定的 但是,当在模拟器中选择“颜色混合层”选项时,这些UILabel将标记为红色而不是绿色 还有哪些选项会导致这些问题 我的代码如下: - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { if (self = [super initWithStyle:style reus
背景色是白色或红色,不透明的都是肯定的
但是,当在模拟器中选择“颜色混合层”
选项时,这些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];
然后它又变绿了
我发现显示中文
UILabel
时会有一个额外的子层
假设您有一个名为titleLabel
的UILabel实例。使用中文或英文设置文本,并使用调试命令检查子层:
po [[titleLabel layer] sublayers]
@习林:到底有什么解决办法吗?还是汉字必须保持红色?@JoeHuang我想这就是UILabel目前的工作方式。也许你可以尝试其他方式来显示文本。我没有对核心文本和其他内容进行实验。@xi.lin发现不错,太棒了!