效果圖:
/**
NSMutableAttributedString類(lèi)可以添加各種樣式,常用的設(shè)置key有:
NSForegroundColorAttributeName 設(shè)置前景色,也就是文本顏色
NSFontAttributeName 設(shè)置字體
NSBackgroundColorAttributeName 設(shè)置背景色
*/
NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:@"藍(lán)色的字沉唠,紅色的背景,天藍(lán)藍(lán),草綠綠翎嫡,一片一片又一片"];
[str addAttribute:NSForegroundColorAttributeName
value:[UIColor blueColor]
range:NSMakeRange(0,10)];
[str addAttribute:NSBackgroundColorAttributeName
value:[UIColor redColor]
range:NSMakeRange(0, 10)];
[str addAttribute:NSForegroundColorAttributeName
value:[UIColor cyanColor]
range:NSMakeRange(11,12)];
[str addAttribute:NSForegroundColorAttributeName
value:[UIColor greenColor]
range:NSMakeRange(19,6)];
[str addAttribute:NSFontAttributeName
value:[UIFont fontWithName:@"Arial" size:30.0]
range:NSMakeRange(0, 10)];
[str addAttribute:NSFontAttributeName
value:[UIFont fontWithName:@"Arial" size:12.0]
range:NSMakeRange(11, 12)];
[str addAttribute:NSFontAttributeName
value:[UIFont fontWithName:@"Arial" size:30.0]
range:NSMakeRange(19, 6)];
UILabel *attrLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 150, 320 - 40, 120)];
attrLabel.attributedText = str;
attrLabel.numberOfLines = 0;
[self.view addSubview:attrLabel];