2

I would like to create a UILabel with varying font sizes, it has to be one UILabel, and I just cant figure out how to accomplish that. I heard about attributed strings and Three20 but I just can not manage to understand how to use them!

I would like to create a UI label that contains a price followed by a small currency sign, for example:

enter image description here

Does any one know how I can achieve this effect in a light and efficient way?

Itamar
  • 1,290
  • 1
  • 18
  • 29
  • 2
    Possible duplicate of http://stackoverflow.com/questions/1417346/iphone-uilabel-containing-text-with-multiple-fonts-at-the-same-time – yuji Feb 12 '12 at 22:57
  • 1
    "it has to be one UILabel" - why? – bneely Feb 12 '12 at 23:12
  • Agree with @yuji. `UILabel`s don't take `NSAttributedString`s. Neither do `UITextField`s or `UITextView`s. You're into Core Text with them and it's ugly. The solutions in the ref'd question are much easier than diving into Core Text. – smparkes Feb 12 '12 at 23:16
  • @iLyrical Is there some specific reason that the answers at [the question yuju linked](http://stackoverflow.com/q/1417346/927947) won't work for you? – NJones Feb 12 '12 at 23:43
  • I will try the anwswer he linked and I will update here as soon as I can, thanks! @yuji – Itamar Feb 13 '12 at 10:48

1 Answers1

0

Here is how to do it:

NSMutableAttributedString * attStr = [[NSMutableAttributedString alloc] initWithString:@"$5.50"];
[attStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:12.0] range:NSMakeRange(0, 1)];
[attStr addAttribute:NSForegroundColorAttributeName value:[UIColor grayColor] range:NSMakeRange(0, 1)];
[attStr addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:17.0] range:NSMakeRange(1, 4)];
[attStr addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:NSMakeRange(1, 4)];  

And don't forget to use setAttributedText instead of setText :

[myLabel setAttributedText:attStr];
Just Shadow
  • 10,860
  • 6
  • 57
  • 75