New*_*der 40 title textcolor uibutton ios
我创建了一个按钮.标题的颜色默认为黑色.但是当我按下它时,颜色会变成蓝色,再也不会变回来,这是怎么发生的?谁能告诉我为什么?我希望按钮的标题始终保持黑色.我怎样才能做到这一点?我试过了
[button setTitleColor:[UIColor darkTextColor] forState:UIControlStateHighlighted];
[button setTitleColor:[UIColor darkTextColor] forState:UIControlStateSelected];
Run Code Online (Sandbox Code Playgroud)
但是没有效果.当我在我的代码中添加它时,按钮的标题似乎总是蓝色.
代码如下.
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setFrame:CGRectMake(20, 360, 280, 44)];
[button setTitle:NSLocalizedString(@"Continue", @"Label: TextLabel in Continue button") forState:UIControlStateNormal];
button.titleLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:20.0f];
button.titleLabel.textColor = [UIColor darkTextColor];
button.titleLabel.shadowColor = [UIColor blackColor];
button.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin|UIViewAutoresizingFlexibleBottomMargin|UIViewAutoresizingFlexibleWidth;
[self.view addSubview:button];
[button release];
Run Code Online (Sandbox Code Playgroud)
感谢大家.我解决了这个问题.我认为根本原因是
button.titleLabel.textColor = [UIColor darkTextColor];
Run Code Online (Sandbox Code Playgroud)
当我删除它,并使用
button setTitleColor:(UIColor) forState:(UIControlState);
Run Code Online (Sandbox Code Playgroud)
问题已经解决了!
PJR*_*PJR 53
您可以使用
[UIButton setTitleColor:forState:]
对于所有州,那么所有州的标题颜色将保持相同.
[button setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
[button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[button setTitleColor:[UIColor redColor] forState:UIControlStateSelected];
Run Code Online (Sandbox Code Playgroud)
注意:要避免键入或粘贴上面的代码三次,您可以使用Will建议的以下代码,
[button setTitleColor:[UIColor redColor] forState:(UIControlStateHighlighted | UIControlStateNormal | UIControlStateSelected)];
Run Code Online (Sandbox Code Playgroud)
bra*_*ipt 29
正如@null指出的那样,到目前为止,最简单的方法是将Interface Builder(或代码中)的按钮类型设置为"Custom".
如果需要使用标准按钮复制此行为,请覆盖该setHighlighted方法以防止titleLabel的alpha通道也进行调整:
- (void)setHighlighted:(BOOL)highlighted
{
[super setHighlighted:highlighted];
self.titleLabel.alpha = 1.0;
}
Run Code Online (Sandbox Code Playgroud)
Swi*_*ect 23
使用Interface Builder和.XIB或.storyboard,选择您UIButton的IB:
视图>实用程序>显示属性检查器.
选择State Config(默认)为Highlighted,Selected或Disabled之一,然后更改Text Color属性.

有一些评论指出了这一点,但为了将其作为实际答案:
将按钮类型设置为Custom在故事板或代码中:
[UIButton buttonWithType:UIButtonTypeCustom];
| 归档时间: |
|
| 查看次数: |
42714 次 |
| 最近记录: |