以编程方式更改UIButton的标题颜色,其标题在iOS 7中设置为属性 [英] Programmatically change title color of UIButton whose title set as attributed in iOS 7

查看:299
本文介绍了以编程方式更改UIButton的标题颜色,其标题在iOS 7中设置为属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have added one UIButton in my UITableView programmatically. My problem is i need to give the Letter Spacing as well as need to change the button title color. I have given the Letter Spacing in button title text using below code, but title text color is not changing.

这是我的代码:

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];

请告诉我如何更改按钮标题颜色?谢谢。

Can you please help me how to change the button title color here? Thanks.

推荐答案

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

I got answer with help of @Larme.

只需添加以下行:

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

感谢所有!!

这篇关于以编程方式更改UIButton的标题颜色,其标题在iOS 7中设置为属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆