如何使用 iOS WatchKit 将 WKInterfaceButton 的标题文本居中

how to center title text for WKInterfaceButton with iOS WatchKit

我试过这个:

[myWKButton.titleLabel setTextAlignment: NSTextAlignmentCenter];

我收到错误:

property titlelabel not found on object of type wkinterfacebutton

我怎样才能做到这一点?

WKInterfaceButton class 没有 titleLabel 属性,这就是您收到此错误的原因。要设置标题的对齐方式,使用它的attributedTitle 属性.

NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.alignment = NSTextAlignmentCenter;

NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString:yourTitle attributes:@{NSParagraphStyleAttributeName: paragraphStyle}];
[myWKButton setAttributedTitle:attributedString];