直接上代码
UILabel *lab = [[UILabelalloc] init];
lab.backgroundColor = [UIColorwhiteColor];
lab.frame =CGRectMake(10.0,40.0, 400, 100);
[self.viewaddSubview:lab];
// 给label添加属性字符串
NSMutableAttributedString *attStr = [[NSMutableAttributedStringalloc] initWithString:@"可变属性字符串例子,谢谢"];
// 颜色
[attStr addAttribute:NSForegroundColorAttributeNamevalue:[UIColorredColor] range:NSMakeRange(1,2)];
// 字体
[attStr addAttribute:NSFontAttributeNamevalue:[UIFontsystemFontOfSize:10.0]range:NSMakeRange(4,3)];
// 下划线
[attStr addAttribute:NSUnderlineStyleAttributeNamevalue:[NSNumbernumberWithInt:NSUnderlineStyleSingle]range:NSMakeRange(5,4)];
lab.attributedText = attStr;
补充:
// 行间距设置
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
[paragraphStyle setLineSpacing:20];
[attStr addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [attStr length])];