0

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!

Eilon
  • 2,698
  • 3
  • 16
  • 32

2 Answers2

0

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];
Kumar
  • 1,882
  • 2
  • 27
  • 44
  • You can create two diff. UILabel each one for userName (diff. color and font style) and for comment string. – Kumar Jun 17 '15 at 12:08
  • Yes but then I won't be able to get the effect I showed in the example. – Eilon Jun 17 '15 at 12:25
0

set an NSAttributedString to the UILabel

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

Halpo
  • 2,982
  • 3
  • 25
  • 54
  • As I've mentioned, I did try that but since my app supports different languages and different writing directions this method glitches. – Eilon Jun 17 '15 at 12:27
  • 1
    it won't glitch if you implement it properly - just need more details about this 'glitching' – Halpo Jun 17 '15 at 12:27
  • If I set my label alignment to be left to right for example, in English it would like fine, like that: `USERNAME content content` but in a right to left language that would look like `content content USERNAME`, which looks very wrong. I need it to be unified. – Eilon Jun 17 '15 at 12:39