简体   繁体   English

使用NSMutableAttributedString更改WKInterfaceLabel的文本颜色

[英]Change text color of WKInterfaceLabel using NSMutableAttributedString

I'm trying to change text color in WKInterfaceLabel using setAttributedText property. 我正在尝试使用setAttributedText属性更改WKInterfaceLabel中的文本颜色。 Here's the code: 这是代码:

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];

The result: 结果:

在此输入图像描述

Only the first attribute works correctly. 只有第一个属性可以正常工作。 I've done some tests but nothing happens, the color font doesn't change to red. 我做了一些测试,但没有任何反应,颜色字体不会变为红色。

WKInterfaceController code: 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 code: MyRowController代码:

@import WatchKit; @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

Could you post your full interface controller and the row controller? 你可以发布完整的界面控制器和行控制器吗? It's very difficult to tell what could be wrong. 很难说出什么可能是错的。 What you are doing at the moment looks fine. 你现在正在做什么看起来很好。 With that said, there are two things that may help here. 话虽如此,这里有两件事可能会有所帮助。

First, make sure not to try to set this in init or awakeWithContext . 首先,确保不要尝试在initawakeWithContext设置它。 You need to set this in willActivate . 您需要在willActivate设置它。 Otherwise you will get some odd behavior. 否则你会得到一些奇怪的行为。

Second, you need to set this when the interface controller is visible. 其次,您需要在界面控制器可见时设置此项。 Otherwise the change may not get picked up. 否则,可能无法获得更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM