2016-02-18 50 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;