如何以编程方式设置WKInterfaceButton字体?

时间:2015-03-26 07:18:23

标签: objective-c swift apple-watch

目前我可以通过故事板调整字体类型和大小。但是,我想以编程方式为不同的场景设置WKInterfaceButton字体。

2 个答案:

答案 0 :(得分:2)

您可以通过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];

答案 1 :(得分:0)

在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)
    }

Demo App