使用 NSMutableAttributedString 更改 WKInterfaceLabel 的文本颜色 [英] Change text color of WKInterfaceLabel using NSMutableAttributedString

查看:30
本文介绍了使用 NSMutableAttributedString 更改 WKInterfaceLabel 的文本颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 setAttributedText 属性更改 WKInterfaceLabel 中的文本颜色.代码如下:

I'm trying to change text color in WKInterfaceLabel using setAttributedText property. 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];

结果:

只有第一个属性可以正常工作.我做了一些测试,但没有任何反应,颜色字体没有变成红色.

Only the first attribute works correctly. I've done some tests but nothing happens, the color font doesn't change to red.

WKInterfaceController 代码:

WKInterfaceController code:

@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 代码:

MyRowController code:

@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.

首先,确保不要尝试在 initawakeWithContext 中设置它.您需要在 willActivate 中进行设置.否则你会得到一些奇怪的行为.

First, make sure not to try to set this in init or awakeWithContext. You need to set this in 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.

这篇关于使用 NSMutableAttributedString 更改 WKInterfaceLabel 的文本颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆