如何让一个控件的标题或者文字多样化显示,这其实很简单,知道NSMutableAttributedString这个类的使用,就知道怎么改了,但大多数人都不知道这个到底怎么使用,当时我做项目时查了很多资料才查到,现在将这个方法分享给大家,希望给你带来帮助
代码如下
-(void)createLabel{
self.view.backgroundColor=[UIColor darkGrayColor];
UILabel*changLabel=[[UILabel alloc]initWithFrame:CGRectMake((self.view.frame.size.width-200)/2.0, 200, 200, 30)];
changLabel.text=@"一个label显示多种字体颜色";
NSMutableAttributedString*attributeStr=[[NSMutableAttributedString alloc]initWithString:changLabel.text];
[attributeStr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(2, 5)];
[attributeStr addAttribute:NSForegroundColorAttributeName value:[UIColor yellowColor] range:NSMakeRange(9, 4)];
[attributeStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:25] range:NSMakeRange(2, 5)];
[attributeStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:10] range:NSMakeRange(9, 4)];
changLabel.attributedText=attributeStr;
[self.view addSubview:changLabel];
}
效果图
看完之后相信你应该知道怎么使用了,也应该知道不管是label,button等控件的使用方法和这个应该是一样!!