如何讓一個控件的標題或者文字多樣化顯示,這其實很簡單,知道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等控件的使用方法和這個應該是一樣!!