1、创建对象
1 2 3 | NSString *original = @"今天你还好吗?"; NSMutableAttributedString *attrTitle = [[NSMutableAttributedStringalloc] initWithString:original]; |
2、设置属性(NSForegroundColorAttributeName代表要设置颜色, value代表值, range 代表范围)
1 2 3 4 5 6 7 8 9 10 11 12 | [attrTitle addAttribute:NSForegroundColorAttributeName value:[UIColorblueColor] range:NSMakeRange(0, 2)]; /** 其他设置: 1.NSForegroundColorAttributeName //颜色 2.NSFontAttributeName //字体 3.NSBackgroundColorAttributeName //背景色 //还有其他的很多的属性,可以自己去看苹果的API,这里不再详述 */ |
3、添加到Label中
1 2 3 4 5 6 7 | UILabel *label = [[UILabel alloc] init]; label.frame = CGRectMake(100, 100, 100, 40); [label setAttributedText:attrTitle]; [self.view addSubview:label]; |