Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UILabel is marked as red when `Color Blended Layers` is selected

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 enter image description here

like image 316
xi.lin Avatar asked Jan 20 '16 09:01

xi.lin


Video Answer


1 Answers

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]
like image 135
xi.lin Avatar answered Oct 02 '22 01:10

xi.lin