使用 NSMutableAttributedString 更改 WKInterfaceLabel 的文本颜色
Change text color of WKInterfaceLabel using NSMutableAttributedString
我正在尝试使用 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
你能post你的全界面控制器和行控制器吗?很难说出哪里出了问题。你现在正在做的事情看起来不错。话虽如此,这里有两件事可能会有所帮助。
首先,请确保不要尝试在 init
或 awakeWithContext
中设置此项。您需要在 willActivate
中进行设置。否则你会得到一些奇怪的行为。
其次,您需要在界面控制器可见时进行设置。否则找零可能得不到。
我正在尝试使用 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
你能post你的全界面控制器和行控制器吗?很难说出哪里出了问题。你现在正在做的事情看起来不错。话虽如此,这里有两件事可能会有所帮助。
首先,请确保不要尝试在 init
或 awakeWithContext
中设置此项。您需要在 willActivate
中进行设置。否则你会得到一些奇怪的行为。
其次,您需要在界面控制器可见时进行设置。否则找零可能得不到。