Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I set a UILabel to expand to fit an arbitrary amount of text?

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

like image 915
dugla Avatar asked Dec 07 '25 08:12

dugla


1 Answers

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
like image 69
Jeff Kelley Avatar answered Dec 10 '25 06:12

Jeff Kelley



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!