【发布时间】:2014-08-22 21:30:50
【问题描述】:
我以编程方式在我的UITableView 中添加了一个UIButton。我的问题是我需要提供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];
您能帮我在这里更改按钮标题颜色吗?谢谢。
【问题讨论】:
-
添加这一行:
[attributedString addAttribute:NSForegroundAttributeName value:[UIColor whiteColor] range:NSMakeRange(0, [@"LOG IN" length])]; -
哇太棒了..它的工作。只需将 NSForegroundAttributeName 改为 NSForegroundColorAttributeName。非常感谢@Larme
-
我的错,忘记了名称属性中的“颜色”。嗯,原因是因为
attributedTitle是一个“新”属性,而titleColor适用于UIButton的title属性,不适用于attributedTitle。
标签: iphone ios7 uibutton nsattributedstring