0

Here is my problem. I have a ViewController in which there is a label with a text and I want to change the color of some of the words in that sentence. The string is an NSLocalizedString which is written in different languages and changes based on the user system language.

    self.welcomeMessageLabel.text = NSLocalizedString(@"welcome_message", nil);

This is the result that I want to achieve. enter image description here

How can I color part of the text?

  • you can use NSMutableAttributedString to change the attributes like font size BGColor for differnt ranges in a string – vishnu anilkumar Aug 30 '21 at 09:53
  • `NSAttrributedString` to handle multiple colors in a text. For finding which one, I'd recommand to use HTML code (and then a HTML parser), or mark the text with tags, like `... un [c]link[/c] all'indirizzo ...`, and parse the string to color them (and bold them too?) – Larme Aug 30 '21 at 09:54

2 Answers2

0

NSLocalizedString(@"welcome_message", nil) returns a NSString.

Let's clarify, it's just a "suite of characters", there is no notion of bold/colors, italic, offsets, etc.

And to show different characters with different rendering (colors, boldness, etc.) you need to use NSAttributedString

Since it's only a suite of characters, you need to find which elements need to have a different rendering. To do so, you can use tags, like HTML, Markdown, BBCode tags.

Sample for the part, and I'll simplify focusing only in bold:

//Initial text
...un link all'indirizzo...
// With BBCode tag
...un [b]link[/b] all'indirizzo...
// With HTML tag
...un <b>link</b> all'indirizzo...
// With Markdown tag
...un **link** all'indirizzo...
// With custom tag
...un {link} all'indirizzo...

Put that new value in your strings file.

If you use HTML, there is a built-in init method for that.
See related question: Convert HTML to NSAttributedString in iOS

For the other ones, you can either use a third party lib parser, or parse them yourself. You can use NSRegularExpression, NSScanner, etc... and then apply the target effect to the correct range.

Larme
  • 24,190
  • 6
  • 51
  • 81
0

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _welcomeMessageLabel.attributedText = [self attributedString1];
}


- (NSAttributedString *)attributedString1 {
    NSString *string = @"Ti abbiamo inviato un link all'inndirizzo email che";
    NSString *substring1 = @"link";
    NSString *substring2 = @"email";
    
//    NSString *string = NSLocalizedString(@"welcome_message", nil);
//    NSString *substring1 = NSLocalizedString(@"welcome_message_substring1", nil);
//    NSString *substring2 = NSLocalizedString(@"welcome_message_substring2", nil);


    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString: string];
    NSDictionary *attributes = @{NSForegroundColorAttributeName: [UIColor orangeColor]};
    
    [attributedString
     replaceCharactersInRange:[string rangeOfString:substring1]
     withAttributedString:[[NSAttributedString alloc] initWithString: substring1 attributes:attributes]
     ];
    
    [attributedString
     replaceCharactersInRange:[string rangeOfString:substring2]
     withAttributedString:[[NSAttributedString alloc] initWithString: substring2 attributes:attributes]
     ];
    
    return  attributedString;
}

@end

enter image description here

  • This work in this particuliar case, but for instance, "link" appears twice in the text. If you wanted to color the second one, you might have failed, since `rangeOf:` returns the first occurence found only. – Larme Aug 30 '21 at 17:11