Pha*_*inh 3 objective-c uibutton ios
我有一个UIButton,我想使颜色的部分UIButton题目,所以我创建NSMutableAttributedString然后setAttributedTitle为按钮。
之后,颜色工作正常,但我无法更改标题 UIButton
如果我不使用setAttributedTitle(通过注释行[self.btnFanLevel setAttributedTitle:text forState:UIControlStateNormal]);),UIButton标题可以改变
这是我用来更改UIButton标题颜色和文本的函数
-(void)changeFanLevel: (NSString *) fanLevelTitle withColor:(UIColor *) color{
NSLog(@"fanLevelTitle = %@", fanLevelTitle);
[self.btnFanLevel setTitle:fanLevelTitle forState:UIControlStateNormal];
NSMutableAttributedString *text =
[[NSMutableAttributedString alloc]
initWithAttributedString: self.btnFanLevel.titleLabel.attributedText];
NSLog(@"text = %@", self.btnFanLevel.titleLabel.attributedText);
[text addAttribute:NSForegroundColorAttributeName
value:color
range:NSMakeRange(10, 3)];
[self.btnFanLevel setAttributedTitle:text forState:UIControlStateNormal];
}
Run Code Online (Sandbox Code Playgroud)
我做错了什么吗?任何帮助,将不胜感激。
更新
我通过在设置标题之前添加这一行来解决我的问题UIButton
[self.btnFanLevel setAttributedTitle:nil forState:UIControlStateNormal];
任何给定 controlState 的属性标题会覆盖该控件状态的标题。因此,一旦您为 UIControlStateNormal 设置了一个属性标题(例如),除非您将该控件状态的属性标题设置为 nil,否则 setTitle 将不会对该控件状态进行任何更改。
| 归档时间: |
|
| 查看次数: |
1722 次 |
| 最近记录: |