views:

343

answers:

2

This question is more for development purposes and would never be used for a shipping app. I need to do a quick and dirty UILabel whose text field I can point to a really, really, long string. Really long. I've ben futzing in IB and can't figure out the correct magic handshake to allow a UILabel to balloon to a really large size. Does anyone have any ideas?

Cheers, Doug

+3  A: 

You can use a method in the UIKit additions to NSString to achieve this. You can get the size using this method:

- (CGSize)sizeWithFont:(UIFont *)font
           minFontSize:(CGFloat)minFontSize
        actualFontSize:(CGFloat *)actualFontSize
              forWidth:(CGFloat)width
         lineBreakMode:(UILineBreakMode)lineBreakMode

Set the size of the label to the return value of this method and you're all set.

EDIT: That method is for one-line strings. If your string is more than one line, use this method:

- (CGSize)sizeWithFont:(UIFont *)font
     constrainedToSize:(CGSize)size
         lineBreakMode:(UILineBreakMode)lineBreakMode
Jeff Kelley
Excellent. Thanks so much Jeff.Cheers,Doug
dugla
No problem, Doug. How about marking it as accepted so I get the points? ;-)
Jeff Kelley
Whoops, forgot. Done. Cheers.
dugla
A: 

Is there a reason why you don't create the label as large as you have space on the screen to begin with? If you are expanding, you will have to give it lots of room anyway, so you might as well have it start out being as large as you can allot to it, and that way you can have some better control of what font to start out and end with, while resizing would make the font resizing a little wonky.

Just curious why you'd rather resize than to create it large to begin with.

mahboudz
this should be a comment, its not an answer
Brock Woolf
Uh huh,mahboudz, I'm just in a hackish state of mind and feel like spending a few moments abusing UILabels ;-).
dugla