简体   繁体   中英

How to set UIlabel TextColor Dynamically

I have created more than one label using this code,

.H file

@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;
}

And than i set background color and textcolor in specific table using this code

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

It will update background of label fine but when i use this code to update textcolor it will display error like this

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIView setTextColor:]: unrecognized selector sent to instance 0xbaa3b30'

Any ways to set textcolor? Plz help.

Start your label tag from 1 instead of 0, because by default the tag value is 0 for all view.

lbl.tag = i+1; 

Because

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

it will return containerView itself, and UIView not have textColor property :P

[containerView viewWithTag:2];

check container view has only one view that is a label with tag 2. May be some other view is set with tag 2 and may be causing the issue.

NSLog(@"%@",label);

can give you what the label here gives the output .It should point out an UILabel itself

make use of isKindOfClass: to identify it is a label you are updating

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

check using this code ;)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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