1

For example, I would like to display something like this:

It is myboldtext label.

But It is my's length cannot be expected, because, it may become

Oh! It is my bold text label.

So, I can't use 3 labels to do so. Any recommends? Thanks.

WendiKidd
  • 4,333
  • 4
  • 33
  • 50
DNB5brims
  • 29,344
  • 50
  • 131
  • 195
  • possible duplicate of [Have half a UILabel text bold and half not?](http://stackoverflow.com/questions/7180805/have-half-a-uilabel-text-bold-and-half-not) – Thilo Aug 18 '12 at 04:10
  • @Thilo It might seem a dupe at first glance but I'm not convinced it is; the popularly accepted answer to this question is that, without 3rd party classes, the only way to do it is with multiple labels. Unlike the question you linked and some others, Ted's question displays a prior understanding of this; he knows multiple labels are the common answer, but doesn't think they'll work in his case. (Not true, as I've shown below). But IMO it's not quite a dupe because of this distinction. (Though of course we can agree to disagree ^^) – WendiKidd Aug 18 '12 at 04:21
  • i think this is what you are looking for http://stackoverflow.com/questions/3586871/bold-non-bold-text-in-a-single-uilabel – Abhigyan Apr 06 '13 at 10:24

3 Answers3

2

UILabel cannot do this out of the box.

I recommend using OHAttributedLabel, which is available on github.

Community
  • 1
  • 1
Thilo
  • 257,207
  • 101
  • 511
  • 656
1

You can still use 3 labels :)

Assuming the It is my label is label1, write a wrapper function that you always call to change label1's text; this function should also recalculate label1's size and reposition label2 and label3.

-(void) setLabel1Text(NSString* newText)
{
    label1.text = newText;
    int label1TextLength = [label1.text sizeWithFont:label1.font];

    label2.x = label1.x + label1TextLength;
    label3.x = label2.x + label2.width;
}

And voila :)

WendiKidd
  • 4,333
  • 4
  • 33
  • 50
  • works, but rather inflexible if you suddenly need more than three ranges. NSAttributedString-based solutions are better IMHO. Hopefully iOS 6 will have better support for it. – Thilo Aug 18 '12 at 04:41
-1

Using a 'UILabel', this is not strictly possible. You can use bold fonts, however, if you want all text to be bold. Reference thithis page for a list of fonts. For example, instead of Helvetica, you can use Helvetica-Bold. With plain UILabels, however, formatting is all or nothing; all the text has to be bold, or none of it is bold. To display rich text with some parts bold, other parts not, however, check out NSMutableAttributedString. Good luck!

James
  • 2,272
  • 1
  • 21
  • 31