uitableview - 选择 `Color Blended Layers`时,UILabel被标记为红色

标签 uitableview uilabel ios-simulator

我有一些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;
}

来自模拟器的快照如下
enter image description here

最佳答案

只是找到一个解决方案:

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/

相关文章:

ios - Swift - 随 UITextView 一起增长的静态 UITableView

objective-c - 将 NSMutableString 分配给 UILabel 文本

ios - UILabel 和 UITextView 换行符不匹配

ios-simulator - 在 10.9 上使用 Xcode 6 没有可用的 iOS 8.0 模拟器

facebook - Xamarin.Auth iOS9 身份验证 SSL 错误

UITableViewCell - 何时在 iOS 10 中正确设置边界

ios - 如何更改 UITableView 中选定单元格的颜色?

iphone - UITableView titleForSection 字体

objective-c - 从NSDictionary打印具有JSON对象的标签文本

ios - Xcode 中的 iPhone 模拟器是否支持低功耗蓝牙?