Roeliee
Roeliee

Reputation: 221

Changing color of label based on text it holds

I'm trying to change the color of a UILabel depending on the .text value it's holding.

I've come up with this but it doesn't seem to work:

titel.text = [NSString stringWithFormat:@"%@", titelJSON];

    if (titel.text == @"PROBLEEM") {
        titel.textColor = [UIColor redColor];
        NSLog(@"1: %@", titel.textColor);
    } else if (titel.text == @"WAARSCHUWING") {
        titel.textColor = [UIColor orangeColor];
        NSLog(@"2: %@", titel.textColor);
    } else {
        titel.textColor = [UIColor blackColor];
        NSLog(@"3: %@", titel.textColor);
    }

Any thoughts?

I have logs in there to see what the if statement is actually picking.

Upvotes: 0

Views: 191

Answers (1)

Jacob Oettinger
Jacob Oettinger

Reputation: 766

You cannot compare strings using == in objective-c. You should use the isEqualToString message instead. Like [titel.text isEqualToString:@"PROBLEEM"]

Upvotes: 3

Related Questions