3

我正在尝试使用 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
4

1 回答 1

2

你能发布你的完整界面控制器和行控制器吗?很难说可能出了什么问题。你现在正在做的事情看起来很好。话虽如此,这里有两件事可能会有所帮助。

首先,请确保不要尝试将其设置为initor awakeWithContext。您需要将其设置在willActivate. 否则你会得到一些奇怪的行为。

其次,您需要在界面控制器可见时进行设置。否则,更改可能不会被提取。

于 2015-02-26T16:46:05.477 回答