选择"颜色混合图层"时,UILabel标记为红色

xi.*_*lin 5 uitableview uilabel ios-simulator

我有一些backgroundColor白色或红色的UILabel ,不透明都是YES.

但是当Color Blended Layers在Simulator中选择该选项时,这些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;
}
Run Code Online (Sandbox Code Playgroud)

UILabel生成的辅助方法如下:

+ (UILabel *)mt_labelWithFont:(UIFont *)font andColor:(UIColor *)color {
    UILabel *label = [UILabel new];
    label.font = font;
    label.textColor = color;
    return label;
}
Run Code Online (Sandbox Code Playgroud)

来自模拟器的快照如下 在此输入图像描述

xi.*_*lin 13

找到一个解决方案:

titleLabel.clipsToBounds = YES;
titleLabel.backgroundColor = [UIColor whiteColor];
Run Code Online (Sandbox Code Playgroud)

然后它再次变绿.


我发现显示中文的链接UILabel会有一个额外的子图层.

假设您有一个名为UILabel的实例titleLabel.使用中文或英文设置文本,并使用debug命令检查子图层:

po [[titleLabel layer] sublayers]
Run Code Online (Sandbox Code Playgroud)