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

Ana*_*tam 3 iphone uibutton nsattributedstring ios7

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

这是我的代码:

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];
Run Code Online (Sandbox Code Playgroud)

你能帮我解决一下如何更改按钮标题颜色吗?谢谢.

Ana*_*tam 12

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

只需添加此行:

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

谢谢大家!!