iphone - 以编程方式更改 UIButton 的标题颜色,其标题设置为 iOS 7 中的属性

标签 iphone ios7 uibutton nsattributedstring

我以编程方式在我的 UITableView 中添加了一个 UIButton。我的问题是我需要给出字母间距以及需要更改按钮标题颜色。我使用下面的代码在按钮标题文本中给出了字母间距,但标题文本颜色没有改变。

这是我的代码:

btnLogin = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btnLogin.frame = CGRectMake(0, 0, 320, 42);
btnLogin.titleLabel.font = customFont;

NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:@"LOG IN"];

[attributedString addAttribute:NSKernAttributeName
                         value:@(spacing)
                         range:NSMakeRange(0, [@"LOG IN" length])];

[btnLogin setAttributedTitle:attributedString forState:UIControlStateNormal];

btnLogin.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"cell_highlighted.png"]];

[btnLogin setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; //it's not working.

[btnLogin addTarget:self action:@selector(onClickLogin) forControlEvents:UIControlEventTouchUpInside];

[cell addSubview:btnLogin];
[cell.contentView bringSubviewToFront:btnLogin];

您能帮助我如何更改此处的按钮标题颜色吗?谢谢。

最佳答案

我在@Larme 的帮助下得到了答案。

只需要添加这一行:

[attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor whiteColor] range:NSMakeRange(0, [@"LOG IN" length])];

谢谢大家!!

关于iphone - 以编程方式更改 UIButton 的标题颜色,其标题设置为 iOS 7 中的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24525224/

相关文章:

ios - 如何以编程方式更改添加到 iOS 上的 NIB 的 UIButton 的背景图像?

iOS如何添加单选按钮

iphone - 1 个用于多个应用程序的推送通知 SSL 证书

ios - UIWebView - 如何在 iPad 上显示 iPhone 大小的内容

ios - 我的开发人员退出,如何编辑并继续开发 ipa 文件(Half Built iOS App)

ios - 如何设置 UIViewController "extend edges"属性

ios - 在 Xcode 5 的 plist 中看不到 "Fonts provided by application"

ios - 带边缘插入的 UIButton 不遵循内容压缩阻力属性

iphone - iOS 设备因未知原因崩溃

ios - ContainerView childViewControlllers View 框架占据整个屏幕