Search code examples
iosobjective-cuikitnsattributedstring

Why doesn't this properly color the substring in my UILabel?


I want a particular substring to be yellow. I don't want to break it up into multiple UILabels since that would make localizing the layout a nightmare. So I do this:

    NSMutableAttributedString* instructions = [[[NSMutableAttributedString alloc] initWithString:self.o_instructionsLabel.text] autorelease];
    NSRange range = [instructions.string rangeOfString:@"FOOBARBAZ"];
    if (range.length > 0)
    {
        [instructions addAttribute:NSForegroundColorAttributeName value:[UIColor yellowColor] range:range];
        self.o_instructionsLabel.attributedText = instructions;
    }

However, the whole of the text in the label remains white. This seems to be how all the examples do it, I verified the range is correct, and when I dump the instructions object, I see the attributes inline where I guess they should be.

What am I doing wrong?


Solution

  • The solution was custom code in drawTextInRect: was ignoring the attributed string.