我正在尝试使用 setAttributedText 属性更改 WKInterfaceLabel 中的文本颜色。这是代码:
MyRowController *row = [self.interfaceTable rowControllerAtIndex:idx];
NSString *str_tmp = @"Test";
NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:str_tmp];
[text addAttribute:NSFontAttributeName value:[UIFont fontWithName:FONT_REGULAR size:12.0] range:NSMakeRange(0, str_tmp.length)];
[text addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, str_tmp.length)];
[row.lines setAttributedText:text];
结果:
只有第一个属性可以正常工作。我做了一些测试,但没有任何反应,颜色字体没有变为红色。
WKInterfaceController 代码:
@interface MyInterfaceController()
@implementation MyInterfaceController
- (void)awakeWithContext:(id)context {
[super awakeWithContext:context];
}
- (void)willActivate {
[super willActivate];
[self loadTableData];
}
- (void)didDeactivate {
[super didDeactivate];
}
#pragma mark -
#pragma mark Table
#pragma mark -
- (void)loadTableData {
NSMutableArray *arrItems = [NSMutableArray new];
for(user* usr in self.agenda.users){
[arrItems addObject:usr];
}
[self.interfaceTable setNumberOfRows:arrItems.count withRowType:@"userRow"];
[arrItems enumerateObjectsUsingBlock:^(NSDictionary *dict, NSUInteger idx, BOOL *stop) {
MyRowController *row = [self.interfaceTable rowControllerAtIndex:idx];
NSString *str_tmp = @"Test";
NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:str_tmp];
[text addAttribute:NSFontAttributeName value:[UIFont fontWithName:FONT_REGULAR size:12.0] range:NSMakeRange(0, str_tmp.length)];
[text addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, str_tmp.length)];
[row.lines setAttributedText:text];
}];
}
@end
MyRowController 代码:
@import WatchKit;
@interface MyRowController : NSObject
@property (weak, nonatomic) IBOutlet WKInterfaceSeparator *separator;
@property (weak, nonatomic) IBOutlet WKInterfaceGroup *contentGroup;
@property (weak, nonatomic) IBOutlet WKInterfaceLabel *lines;
@property (weak, nonatomic) IBOutlet WKInterfaceGroup *separatorBottom;
@end