您的位置:首页 > 其它

label字体颜色多样化显示

2015-03-30 12:07 127 查看
如何让一个控件的标题或者文字多样化显示,这其实很简单,知道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等控件的使用方法和这个应该是一样!!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: