我以编程方式在我的 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/