這只是一個(gè)簡單的demo滤蝠,用相同的思路也可以構(gòu)建炫酷的特效,這里就展示最容易的入門代碼
代碼如下:
-
(void)viewDidLoad {
[super viewDidLoad];UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 30, 300, 260)];
label.text = @"Label Text Content, This is a text label things attribute";//默認(rèn)為空
label.font = [UIFont systemFontOfSize:17];//默認(rèn)使用系統(tǒng)的17
label.textColor = [UIColor orangeColor];//默認(rèn)使用文本黑色
label.textAlignment = NSTextAlignmentCenter;//默認(rèn)是左對(duì)齊
[self.view addSubview:label];NSString *string = label.text;
const CGFloat fontSize = 16.0;
NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:string];
NSUInteger length = [string length];
//設(shè)置字體
UIFont *baseFont = [UIFont systemFontOfSize:fontSize];
[attrString addAttribute:NSFontAttributeName value:baseFont range:NSMakeRange(0, length)];//設(shè)置所有的字體
UIFont *boldFont = [UIFont boldSystemFontOfSize:fontSize];
[attrString addAttribute:NSFontAttributeName value:boldFont range:[string rangeOfString:@"Text"]];//設(shè)置Text這四個(gè)字母的字體為粗體
label.attributedText = attrString;// Do any additional setup after loading the view, typically from a nib.
}
對(duì)你有幫助的話煮岁,甩個(gè)贊??吧