iOS 在一个UILabel上显示不同颜色

self.addressLabel = [[UILabel alloc]initWithFrame:CGRectZero];

        self.addressLabel.textColor = [UIColor blackColor];

        self.addressLabel.font = [UIFont systemFontOfSize:15];

        [self.contentView addSubview:self.addressLabel];

        

        NSMutableAttributedString *noteStr = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"[默认地址]哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈"]];

        NSRange redRangeTwo = NSMakeRange([[noteStr string] rangeOfString:@"[默认地址]"].location, [[noteStr string] rangeOfString:@"[默认地址]"].length);

        [noteStr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:redRangeTwo];

        [self.addressLabel setAttributedText:noteStr];

        [self.addressLabel sizeToFit];


你可能感兴趣的:(iOS,UILabel)