UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 20, 520, 100)];
label.textColor = [UIColor grayColor];
NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:@"小明回复:终于可以回家了,太高兴了,哈哈哈哈哈哈。"];
[str addAttribute:NSForegroundColorAttributeName
value:[UIColor blueColor]
range:NSMakeRange(0,5)];
[str addAttribute:NSForegroundColorAttributeName
value:[UIColor redColor]
range:NSMakeRange(6,12)];
[str addAttribute:NSForegroundColorAttributeName
value:[UIColor blackColor]
range:NSMakeRange(2,2)];
//下划线
[str addAttribute:(NSString *)kCTUnderlineStyleAttributeName
value:(id)[NSNumber numberWithInt:kCTUnderlineStyleSingle]
range:NSMakeRange(0, 4)];
[str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Arial-BoldItalicMT" size:30.0] range:NSMakeRange(0, 5)];
[str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"HelveticaNeue-Bold" size:30.0] range:NSMakeRange(6, 12)];
[str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Courier-BoldOblique" size:30.0] range:NSMakeRange(19, 6)];
label.attributedText = str;
本文介绍如何使用Objective-C为UILabel设置不同的文字颜色、字体及下划线等属性,通过具体代码示例展示了NSAttributedString的使用方法。
2932

被折叠的 条评论
为什么被折叠?



