设置 WKInterfaceButton 标题颜色 - WatchKit
set WKInterfaceButton title color - WatchKit
有没有办法以编程方式更改 WatchKit WKInterfaceButton 标题的颜色?我知道我可以在故事板中更改它,但我需要在代码中更改它。
Per Apple documentation 它没有说明是否允许这样的操作。我确实尝试查看 WKInterfaceButton 的所有可用方法,其中有一个用于 setBackgroundColor 但不是用于标题颜色。我错过了什么?
我没试过,但看起来你可以使用 setAttributedTitle:
并且其中一个属性可以是 NSForegroundColorAttributeName
以及你想要设置标题的颜色。
根据文档,您可以在情节提要中设置标题颜色:https://developer.apple.com/library/prerelease/ios/documentation/WatchKit/Reference/WKInterfaceButton_class/(Table 1 – WatchKit 按钮属性)
感谢 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];
另一种选择是将按钮设置为使用 WKInterfaceGroup 作为其内容,并为文本插入标签。
那么你只需在标签上设置TextColor
[[self myLabel] setTextColor:textColor];
像@SamB 的回答一样使用Swift:
let attString = NSMutableAttributedString(string: "NewTitle")
attString.setAttributes([NSForegroundColorAttributeName: UIColor.redColor()], range: NSMakeRange(0, attString.length))
self.button.setAttributedTitle(attString)
希望对你有所帮助。
对于 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)
}
Swift 4
let attString = NSMutableAttributedString(string: "NewTitle")
attString.setAttributes([NSAttributedStringKey.foregroundColor: UIColor.red], range: NSMakeRange(0, attString.length))
self.leftButton.setAttributedTitle(attString)
有没有办法以编程方式更改 WatchKit WKInterfaceButton 标题的颜色?我知道我可以在故事板中更改它,但我需要在代码中更改它。
Per Apple documentation 它没有说明是否允许这样的操作。我确实尝试查看 WKInterfaceButton 的所有可用方法,其中有一个用于 setBackgroundColor 但不是用于标题颜色。我错过了什么?
我没试过,但看起来你可以使用 setAttributedTitle:
并且其中一个属性可以是 NSForegroundColorAttributeName
以及你想要设置标题的颜色。
根据文档,您可以在情节提要中设置标题颜色:https://developer.apple.com/library/prerelease/ios/documentation/WatchKit/Reference/WKInterfaceButton_class/(Table 1 – WatchKit 按钮属性)
感谢 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];
另一种选择是将按钮设置为使用 WKInterfaceGroup 作为其内容,并为文本插入标签。
那么你只需在标签上设置TextColor
[[self myLabel] setTextColor:textColor];
像@SamB 的回答一样使用Swift:
let attString = NSMutableAttributedString(string: "NewTitle")
attString.setAttributes([NSForegroundColorAttributeName: UIColor.redColor()], range: NSMakeRange(0, attString.length))
self.button.setAttributedTitle(attString)
希望对你有所帮助。
对于 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)
}
Swift 4
let attString = NSMutableAttributedString(string: "NewTitle")
attString.setAttributes([NSAttributedStringKey.foregroundColor: UIColor.red], range: NSMakeRange(0, attString.length))
self.leftButton.setAttributedTitle(attString)