這只是一個簡單的demo,用相同的思路也可以構建炫酷的特效,這里就展示最容易的入門代碼
代碼如下:
-
(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";//默認為空
label.font = [UIFont systemFontOfSize:17];//默認使用系統的17
label.textColor = [UIColor orangeColor];//默認使用文本黑色
label.textAlignment = NSTextAlignmentCenter;//默認是左對齊
[self.view addSubview:label];NSString *string = label.text;
const CGFloat fontSize = 16.0;
NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:string];
NSUInteger length = [string length];
//設置字體
UIFont *baseFont = [UIFont systemFontOfSize:fontSize];
[attrString addAttribute:NSFontAttributeName value:baseFont range:NSMakeRange(0, length)];//設置所有的字體
UIFont *boldFont = [UIFont boldSystemFontOfSize:fontSize];
[attrString addAttribute:NSFontAttributeName value:boldFont range:[string rangeOfString:@"Text"]];//設置Text這四個字母的字體為粗體
label.attributedText = attrString;// Do any additional setup after loading the view, typically from a nib.
}
對你有幫助的話,甩個贊??吧