Aldrin Equila
Aldrin Equila

Reputation: 163

How to make a single UILabel with multiple colors

I want to create a single uilabel with 2 colors. the first color is black and the other one is blue.

i could use multiple uilabel in it but i want to have only single uilabel. Is there any way i can implement this?

This should be the output.

enter image description here

and here is my code:

UILabel * lblPostContent = [[UILabel alloc] initWithFrame:CGRectMake((ICON_PADDING*1.5), 42, container.frame.size.width-30, 34)];
lblPostContent.numberOfLines =0;
[lblPostContent setFont:[UIFont systemFontOfSize:11]];
[lblPostContent setText:[NSString stringWithFormat:@"I just scored %d points at %@ using the iBowl app", score, LuckyStrikes]];
[container addSubview:lblPostContent];

Upvotes: 2

Views: 8198

Answers (3)

AntonijoDev
AntonijoDev

Reputation: 1315

Yo should use attributes... like this

NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:yourString];
[text addAttribute: NSForegroundColorAttributeName value: [UIColor blackColor] range: NSMakeRange(0, TXTTOBEBLACKLENGTH)];
[text addAttribute: NSForegroundColorAttributeName value: [UIColor blueColor] range: NSMakeRange(TXTTOBEBLACKLENGTH, TXTTOBEBLBLUELENGTH)];
[lblPostContent setAttributedText: text];

Upvotes: 10

Krunal
Krunal

Reputation: 79776

Here is extension function for AttributedString:

extension NSMutableAttributedString {

    func setColorForText(textToFind: String, withColor color: UIColor) {
        let range: NSRange = self.mutableString.range(of: textToFind, options: .caseInsensitive)
        self.addAttribute(NSAttributedStringKey.foregroundColor, value: color, range: range)
    }

}

Try it using Label:

let label = UILabel()
label.frame = CGRect(x: 40, y: 100, width: 280, height: 200)
let first = "89"
let second = "Lucky Strikes"
let third = "iBowl app"
let stringValue = "I just scored \(first) points at \(second) using the \(third)"  // or direct assign single string value like "firstsecondthird"
label.textColor = UIColor.lightGray
label.numberOfLines = 0
let attributedString: NSMutableAttributedString = NSMutableAttributedString(string: stringValue)
attributedString.setColorForText(textToFind: first, withColor: UIColor.blue)   // use variable for string "89"
attributedString.setColorForText(textToFind: second, withColor: UIColor.blue) // or direct string like this "Lucky Strikes"
attributedString.setColorForText(textToFind: third, withColor: UIColor.blue)
label.font = UIFont.systemFont(ofSize: 26)
label.attributedText = attributedString
self.view.addSubview(label)

Here is result:

enter image description here

Upvotes: 0

Mike Pollard
Mike Pollard

Reputation: 10195

To avoid having to count lengths of parts of your text and generate NSRanges you can compose your final NSMutableAttributedString from component NSAttributedString objects using appendAttributedString: as follows:

UIColor *normalColor = [UIColor blackColor];
UIColor *highlightColor = [UIColor blueColor];
UIFont *font = [UIFont systemFontOfSize:12.0];        
NSDictionary *normalAttributes = @{NSFontAttributeName:font, NSForegroundColorAttributeName:normalColor};
NSDictionary *highlightAttributes = @{NSFontAttributeName:font, NSForegroundColorAttributeName:highlightColor};

NSAttributedString *normalText = [[NSAttributedString alloc] initWithString:@"Normal " attributes:normalAttributes];
NSAttributedString *highlightedText = [[NSAttributedString alloc] initWithString:@"Highlighted" attributes:highlightAttributes];

NSMutableAttributedString *finalAttributedString = [[NSMutableAttributedString alloc] initWithAttributedString:normalText];
[finalAttributedString appendAttributedString:highlightedText];

self.label.attributedText = finalAttributedString;

Upvotes: 6

Related Questions