IOS-同一label显示不同字体颜色

UILabel* noteLabel = [[UILabel alloc] init];
noteLabel.frame = CGRectMake(0, 100, 200, 100);
noteLabel.textColor = [UIColor blackColor];
NSMutableAttributedString *noteStr = [[NSMutableAttributedString alloc] initWithString:@”1111:2222”];
NSRange redRange = NSMakeRange(0, [[noteStr string] rangeOfString:@”:”].location);
[noteStr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:redRange];
[noteLabel setAttributedText:noteStr] ;
[noteLabel sizeToFit];
[self.view addSubview:noteLabel];

补充 NSMutableParagraphStyle *paragraphStyle = [[[NSMutableParagraphStyle alloc] init] autorelease];
[paragraphStyle setLineSpacing:LINESPACE];//调整行间距
[attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [contentStr length])];

换行需要设置numberOfLines = 0

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