2016-02-18 52 views
-1

我通過太多修改顯示長文本。 大文本包含特殊字符。每當特殊字符出現在下面時,應該加粗並且出現\ n應該生成新標籤。在UILabel中的文本之間添加粗體文本

+2

使用NSAttributedString:[http://stackoverflow.com/questions/3482346/how-do-you-use-nsattributedstring](http://stackoverflow.com/questions/3482346/how-do-you -use-nsattributedstring) –

+0

我們不能將普通字符串與歸屬字符串連接起來.. 我需要字符串的som文本以粗體顯示,並且此解決方案我嘗試了.. 不工作 –

回答

2

試試這個:你可以在value參數中添加任何字體樣式。使用這個,你可以添加樣式到子字符串,使它與普通字符串不同。

NSString *strFirst = @"Anylengthtext"; 
NSString *strSecond = @"Anylengthtext"; 
NSString *strThird = @"Anylengthtext"; 

NSString *strComplete = [NSString stringWithFormat:@"%@ %@ %@",strFirst,strSecond,strThird]; 

NSMutableAttributedString *attributedString =[[NSMutableAttributedString alloc] initWithString:strComplete]; 

[attributedString addAttribute:NSForegroundColorAttributeName 
       value:[UIColor whiteColor] 
       range:[strComplete rangeOfString:strFirst]]; 

[attributedString addAttribute:NSForegroundColorAttributeName 
       value:[UIFont fontWithName:@"Roboto-Regular" size:12] 
       range:[strComplete rangeOfString:strSecond]]; 

[attributedString addAttribute:NSForegroundColorAttributeName 
       value:[UIColor blueColor] 
       range:[strComplete rangeOfString:strThird]]; 


self.lblName.attributedText = attributedString;