如何动态设置UILabel的文字颜色

3
我使用以下代码创建了多个标签:
.H文件
@interface ViewController : UIViewController
{
    NSArray * phraseAry ;
    UIView * containerView;
    UILabel * oldLabel;
    NSMutableArray *dataArray;
}
@property (strong, nonatomic) IBOutlet UIScrollView *myScrollView;

.M file

- (void)viewDidLoad
{
    [super viewDidLoad];

    heightValue = 20;
    widthValue = 0;
    xValue = 5;
    yValue = 10;

containerView = [[UIView alloc] init];
    for (int i=0; i<phraseAry.count; i++) {
        widthValue = [self returnWidth:[phraseAry objectAtIndex:i]];

        int newXValue = xValue+widthValue+5;

        //NSLog(@"newXValue : %i",newXValue);
        if (newXValue > 310) {
            yValue +=20;
            xValue = 5;
            newXValue = xValue+widthValue+5;
            UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(xValue, yValue, widthValue, heightValue)];
            lbl.text = [phraseAry objectAtIndex:i];
            [lbl setFont:[UIFont fontWithName:@"Helvetica" size:14.0]];
            lbl.tag = i;
            lbl.textColor = [UIColor colorWithRed:(92/255.0) green:(109/255.0) blue:(43/255.0) alpha:1];
            [containerView addSubview:lbl];
            xValue = newXValue;
        } else {
            UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(xValue, yValue, widthValue, heightValue)];
            lbl.text = [phraseAry objectAtIndex:i];
            [lbl setFont:[UIFont fontWithName:@"Helvetica" size:14.0]];
            lbl.tag = i;
            lbl.textColor = [UIColor colorWithRed:(92/255.0) green:(109/255.0) blue:(43/255.0) alpha:1];
            [containerView addSubview:lbl];
            xValue = newXValue;
        }
    }

    containerView.frame = CGRectMake(0, 0, 320, yValue);
    //add code to customize, e.g. polygonView.backgroundColor = [UIColor blackColor];
    [self.myScrollView addSubview:containerView];
    self.myScrollView.contentSize = containerView.frame.size;
}

我使用以下代码在特定表格中设置背景颜色和文本颜色。
- (void)updateLabelMethod
 {
            oldLabel.backgroundColor = [UIColor clearColor];
            UILabel *label = (UILabel *)[containerView viewWithTag:2];
            oldLabel = label;
            label.backgroundColor = [UIColor colorWithRed:(98/255.0) green:(147/255.0) blue:(216/255.0) alpha:1];
            label.textColor = [UIColor whiteColor];
            containerView.backgroundColor = [UIColor clearColor];
}

它会成功更新标签的背景,但是当我使用此代码来更新文本颜色时,它会显示如下错误:
终止应用程序,原因:未捕获的异常'NSInvalidArgumentException',原因:'-[UIView setTextColor:]:向实例0xbaa3b30发送无法识别的选择器'
有没有其他方法设置文本颜色?请帮忙。

看起来,你将要强制转换为UILabel的标签实例变量实际上并不属于UILabel类,而是UIView类。 - johnyu
UILabel *label = (UILabel *)[containerView viewWithTag:2]; 使用 isKindOfClass 检查这是否真的是一个标签? - Mike
尝试清除您的项目并重新构建。在我的情况下,它可以正常工作。 - Armaan Stranger
3个回答

3

在标签中,建议从1开始而不是0,因为默认情况下所有视图的标记值都为0。

lbl.tag = i+1; 

因为

UILabel *label = (UILabel *)[containerView viewWithTag:0];  

它会返回containerView本身,而UIView没有textColor属性 :P


谢谢回复,现在我明白了为什么当我像Lithu T.V建议的那样添加nslog时,我的第一个值始终是UIView。再次感谢..+1 - user2452016
但伙计,在实现这个之后,你就不需要使用isKindOfClass了 :D - Rajneesh071

1
    for (UILable *lblTemp in containerView.subviews){
     if ([lblTemp isKindOfClass:[UILable class]] && lblTemp.tag==2){
         // change the lbl color
        break;
     }
  }

检查使用此代码 ;)

我认为这是最好的答案,排序也很好,谢谢,点赞 +1。 - user2452016

1

[containerView viewWithTag:2];

检查容器视图是否只有一个标签为2的标签视图。 可能还有其他视图设置了标签2,可能会引起问题。

NSLog(@"%@",label);

这里的标签可以给出输出结果。它应该指出一个UILabel本身。

使用isKindOfClass:来识别你正在更新的标签是否为标签。


我并没有使用标签来设置视图,而是使用计数器来设置值。例如,我将计数器设置为“2”。 - user2452016
你的 NSLog 代码给了我一个标签列表,该列表将使用计数值进行选择,在我的情况下第一个是视图,我不知道为什么,因为如我所说,除了标签之外,我没有给任何其他对象打标签。因此,我只实现了当计数为 0 时忽略代码的逻辑,并且它起作用了。谢谢... - user2452016

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接