我正在尝试使用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
答案 0 :(得分:2)
您可以发布完整的界面控制器和行控制器吗?很难说出什么可能是错的。你现在正在做什么看起来很好。话虽如此,这里有两件事可能会有所帮助。
首先,请务必不要尝试在init
或awakeWithContext
中进行设置。您需要在willActivate
中进行设置。否则你会得到一些奇怪的行为。
其次,您需要在界面控制器可见时设置此项。否则,可能无法获得更改。