我是iOS新用户。我想知道如何更改UIButton
标题的每个部分的颜色。iOS:我可以更改Button Tittle的颜色部分吗?
例如,标题我UIButton
一部分是黑和另一个标题的一部分,我UIButton
是黄色
任何帮助将是赞赏
我是iOS新用户。我想知道如何更改UIButton
标题的每个部分的颜色。iOS:我可以更改Button Tittle的颜色部分吗?
例如,标题我UIButton
一部分是黑和另一个标题的一部分,我UIButton
是黄色
任何帮助将是赞赏
U可以使用NSMutableAttributedString
然后将其添加到按钮与[button setAttributedTitle:attString forState:UIControlStateNormal];
要更改属性字符串一系列的颜色,阅读this,基本上它只是属性字典
1阅读本SO Question
我用下面的代码解决了我的问题。
对于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];
没有,我无法找到任何教程,但我能做到这一点在Android中 –
的可能的复制[编程方式更改的UIButton的标题,其标题设置在IB作为归因](http://stackoverflow.com/questions/12652198/programmatically-change-title-of-uibutton-whose-title-was-set-in-ib-as-attribute) –
@JasonNam请看我的编辑帖子 –