3

我使用此代码创建了多个标签,

.H 文件

@interface ViewController : UIViewController
{
    NSArray * phraseAry ;
    UIView * containerView;
    UILabel * oldLabel;
    NSMutableArray *dataArray;
}
@property (strong, nonatomic) IBOutlet UIScrollView *myScrollView;

.M 文件

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

它会很好地更新标签的背景,但是当我使用此代码更新 textcolor 时,它会显示这样的错误

由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[UIView setTextColor:]:无法识别的选择器发送到实例 0xbaa3b30”

有什么方法可以设置文本颜色?请帮忙。

4

3 回答 3

3

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

lbl.tag = i+1; 

因为

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

它会返回containerView自己,并且UIView没有textColor属性:P

于 2013-07-23T12:59:55.523 回答
1

[containerView viewWithTag:2];

检查容器视图只有一个视图,该视图是带有标签 2 的标签。可能是其他视图设置了标签 2 并且可能导致问题。

NSLog(@"%@",label);

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

使用 isKindOfClass:来识别它是您正在更新的标签

于 2013-07-23T12:52:40.180 回答
1
    for (UILable *lblTemp in containerView.subviews){
     if ([lblTemp isKindOfClass:[UILable class]] && lblTemp.tag==2){
         // change the lbl color
        break;
     }
  }

使用此代码进行检查;)

于 2013-07-23T13:04:51.810 回答