Youtube/Instagram comments style in UILabel

246 Views Asked by At

I'm trying to achieve a Youtube/Instagram-like comment style in my iOS app, or more specifically - a label that has the username at the beggining of the label with a different color and the rest of the content afterwards, and the content can drop lines beneath the username. For example:

USERNAME(with different color)  this is the comment content,
and it can drop lines like that.

I have managed to achieve that using NSMutableAttributedString, but since my app supports multiple languages with different writing directions it glitches quite badly.
How can I achieve this style in a UILabel? Thanks!

2

There are 2 best solutions below

2
On

You can try two different labels.

or you can use following code

 NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithAttributedString: label.attributedText];

[text addAttribute:NSForegroundColorAttributeName 
                   value:[UIColor redColor] 
                   range:NSMakeRange(0, /*userNameLength*/)];

[label setAttributedText:text];
3
On

set an NSAttributedString to the UILabel

you can set specific fonts / colours etc to a particular range of characters within the NSAttributedString