UILabel with text of two different colors UILabel with text of two different colors objective-c objective-c

UILabel with text of two different colors


The way to do it is to use NSAttributedString like this:

NSMutableAttributedString *text =  [[NSMutableAttributedString alloc]    initWithAttributedString: label.attributedText];[text addAttribute:NSForegroundColorAttributeName              value:[UIColor redColor]              range:NSMakeRange(10, 1)];[label setAttributedText: text];

I created a UILabel extension to do it.


I have done this by creating a category for NSMutableAttributedString

-(void)setColorForText:(NSString*) textToFind withColor:(UIColor*) color{    NSRange range = [self.mutableString rangeOfString:textToFind options:NSCaseInsensitiveSearch];    if (range.location != NSNotFound) {        [self addAttribute:NSForegroundColorAttributeName value:color range:range];    }}

Use it like

- (void) setColoredLabel{    NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:@"Here is a red blue and green text"];    [string setColorForText:@"red" withColor:[UIColor redColor]];    [string setColorForText:@"blue" withColor:[UIColor blueColor]];    [string setColorForText:@"green" withColor:[UIColor greenColor]];    mylabel.attributedText = string;}

SWIFT 3

extension NSMutableAttributedString{    func setColorForText(_ textToFind: String, with color: UIColor) {        let range = self.mutableString.range(of: textToFind, options: .caseInsensitive)        if range.location != NSNotFound {            addAttribute(NSForegroundColorAttributeName, value: color, range: range)        }    }}

USAGE

func setColoredLabel() {    let string = NSMutableAttributedString(string: "Here is a red blue and green text")    string.setColorForText("red", with: #colorLiteral(red: 0.9254902005, green: 0.2352941185, blue: 0.1019607857, alpha: 1))    string.setColorForText("blue", with: #colorLiteral(red: 0.2392156869, green: 0.6745098233, blue: 0.9686274529, alpha: 1))    string.setColorForText("green", with: #colorLiteral(red: 0.3411764801, green: 0.6235294342, blue: 0.1686274558, alpha: 1))    mylabel.attributedText = string}

SWIFT 4 @kj13 Thanks for notifying

// If no text is send, then the style will be applied to full textfunc setColorForText(_ textToFind: String?, with color: UIColor) {    let range:NSRange?    if let text = textToFind{        range = self.mutableString.range(of: text, options: .caseInsensitive)    }else{        range = NSMakeRange(0, self.length)    }    if range!.location != NSNotFound {        addAttribute(NSAttributedStringKey.foregroundColor, value: color, range: range!)    }}

I have did more experiments with attributes and below are the results, here is the SOURCECODE

Here is the result

Styles


Here you go

NSMutableAttributedString * string = [[NSMutableAttributedString alloc] initWithString:lblTemp.text];[string addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0,5)];[string addAttribute:NSForegroundColorAttributeName value:[UIColor greenColor] range:NSMakeRange(5,6)];[string addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(11,5)];lblTemp.attributedText = string;