2015-09-07 86 views
2

如果想要使用属性字符串来格式化字符串,以使第一个字具有比第二个字更多的其他样式(例如字体)。所以我想我可以用NSMutableAttributedString。现在我有一个UIBarButtonItem,但在这里您只能设置标题的标题和文本属性。对UIBarButtonItem中的字符串使用多种样式标题

我看到一个implementation,其中一个NSDictionary用于具有多个属性。这样做的缺点是你只能对整个字符串进行格式化。我需要它作为字符串的一部分。

是否可以在UIBarButtonItem中为同一个标题字符串设置多种样式?

+0

你的问题是一个精细 –

回答

3

您可以初始化一个UIBarButtonItem用自定义视图。准备你的属性字符串,创建一个UILabel,将其attributedText设置为准备好的,然后用标签初始化按钮。

事情是这样的:

UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 24)]; 

NSAttributedString *strt = [[NSAttributedString alloc] initWithString:@"text" 
    attributes:@{ 
       NSForegroundColorAttributeName : [UIColor redColor] 
       }]; 
label.attributedText = strt; 

self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:label]; 
1

您可以像使用

NSMutableAttributedString *;label_text = 
[[NSMutableAttributedString alloc]initWithAttributedString: yourlabelel.attributedText]; 
[label_text addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0,1)]; 
[label_text addAttribute:NSForegroundColorAttributeName value: [UIColor greenColor] range:NSMakeRange(1,2)]; 
[label_text addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(2,3)]; 

[yourlabelel setAttributedText: label_text]; 

终于appened这个标签您UIBarButtonItem

选择-2

看到这个link

+0

NSMakeRange(0,5) - 字符串的长度/ yourLabel.text –

+0

按您刚才的问题我创建的答案就在这里 –

相关问题