这只是一个简单的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.
}
对你有帮助的话,甩个赞👍吧