是否有办法以编程方式更改WatchKit WKInterfaceButton标题的颜色?我知道我可以在故事板中修改它,但是我需要在代码中修改它。
根据苹果文档,它没有说明是否允许这样的操作。我确实尝试过查看WKInterfaceButton的所有可用方法,其中有一个用于setBackgroundColor,但没有用于标题颜色。我错过了什么吗?
发布于 2015-03-20 18:51:14
感谢ColdLogic为我指明了正确的方向。这是一个有点复杂的方式改变标题的颜色,但它的工作。
NSString *titleStr = @"Next";
NSMutableAttributedString *attString = [[NSMutableAttributedString alloc] initWithString:titleStr];
[attString setAttributes:@{NSForegroundColorAttributeName:[UIColor redColor]} range:NSMakeRange(0, attString.string.length)];
//set color
[customBtn setAttributedTitle:attString];发布于 2015-03-20 18:34:23
我还没有尝试过这一点,但是看起来您可以使用setAttributedTitle:,其中一个属性可以是NSForegroundColorAttributeName,它的颜色也可以设置为标题。
发布于 2016-10-16 17:49:35
对于Swift用户,可以为WKInterfaceButton使用扩展
extension WKInterfaceButton {
func setTitleWithColor(title: String, color: UIColor) {
let attString = NSMutableAttributedString(string: title)
attString.setAttributes([NSForegroundColorAttributeName: color], range: NSMakeRange(0, attString.length))
self.setAttributedTitle(attString)
}https://stackoverflow.com/questions/29173471
复制相似问题