如何以编程方式设置 WKInterfaceButton 字体?
How to set WKInterfaceButton font programmatically?
目前我可以通过故事板调整字体类型和大小。但是,我想为不同的场景以编程方式设置 WKInterfaceButton
字体。
您可以通过 NSAttributedString
执行此操作
UIFont * buttonFont = [UIFont fontWithName:@"Courier-Bold" size:6];
NSAttributedString *buttonText = [[NSAttributedString alloc] initWithString : @"Your button title" attributes : @{ NSKernAttributeName : @2.0, NSFontAttributeName : buttonFont}];
[self.button setAttributedTitle:buttonText];
In Swift-3 NSAttributedString 用于更新属性:
func changeAttributeOfText() {
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = .left
let font = UIFont.boldSystemFont(ofSize: 12)
let attributes:Dictionary = [NSParagraphStyleAttributeName:paragraphStyle , NSFontAttributeName:font ]
let attributeString:NSAttributedString = NSAttributedString(string: "HELLO", attributes: attributes)
buttonOutlet.setAttributedTitle(attributeString)
}
目前我可以通过故事板调整字体类型和大小。但是,我想为不同的场景以编程方式设置 WKInterfaceButton
字体。
您可以通过 NSAttributedString
执行此操作
UIFont * buttonFont = [UIFont fontWithName:@"Courier-Bold" size:6];
NSAttributedString *buttonText = [[NSAttributedString alloc] initWithString : @"Your button title" attributes : @{ NSKernAttributeName : @2.0, NSFontAttributeName : buttonFont}];
[self.button setAttributedTitle:buttonText];
In Swift-3 NSAttributedString 用于更新属性:
func changeAttributeOfText() {
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = .left
let font = UIFont.boldSystemFont(ofSize: 12)
let attributes:Dictionary = [NSParagraphStyleAttributeName:paragraphStyle , NSFontAttributeName:font ]
let attributeString:NSAttributedString = NSAttributedString(string: "HELLO", attributes: attributes)
buttonOutlet.setAttributedTitle(attributeString)
}