2015-12-29 37 views
-2

我是iOS新用户。我想知道如何更改UIButton标题的每个部分的颜色。iOS:我可以更改Button Tittle的颜色部分吗?

例如,标题我UIButton一部分是和另一个标题的一部分,我UIButton黄色

这是在描述图像 Here is the button that I want

任何帮助将是赞赏

+0

没有,我无法找到任何教程,但我能做到这一点在Android中 –

+2

的可能的复制[编程方式更改的UIButton的标题,其标题设置在IB作为归因](http://stackoverflow.com/questions/12652198/programmatically-change-title-of-uibutton-whose-title-was-set-in-ib-as-attribute) –

+0

@JasonNam请看我的编辑帖子 –

回答

2

U可以使用NSMutableAttributedString然后将其添加到按钮与[button setAttributedTitle:attString forState:UIControlStateNormal];

要更改属性字符串一系列的颜色,阅读this,基本上它只是属性字典

1阅读本SO Question

+0

你能解释更多,在链接y ou给我吧。它不会谈论改变颜色。 –

+0

你可以阅读链接我更新:)其只是一个属性 – Tj3n

+0

非常感谢你;)。我做到了 –

0

我用下面的代码解决了我的问题。
对于UILabel

NSMutableAttributedString *text = 
[[NSMutableAttributedString alloc] 
initWithAttributedString: self.lblFriendBand.attributedText]; 

[text addAttribute:NSForegroundColorAttributeName 
      value:[UIColor redColor] 
      range:NSMakeRange(1, 2)]; 
[self.lblFriendBand setAttributedText: text]; 

对于UIButton

NSMutableAttributedString *text2 = 
    [[NSMutableAttributedString alloc] 
     initWithAttributedString: self.btnFriendName.titleLabel.attributedText]; 
    [text2 addAttribute:NSForegroundColorAttributeName 
       value:[UIColor redColor] 
       range:NSMakeRange(1, 2)]; 
    [self.btnFriendName setAttributedTitle:text2 forState:UIControlStateNormal]; 
相关问题