Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sizing a UILabel to fit?

Tags:

cocoa-touch

How would one modify the following snippet (in a tableView:cellForRowAtIndexPath: UITableViewController method) from the "09a - PrefsTable" recipe from Chapter 6 of The iPhone Developer's Cookbook:

if (row == 1) {      // Create a big word-wrapped UILabel      cell = [tableView dequeueReusableCellWithIdentifier:@"libertyCell"];      if (!cell) {          cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:@"libertyCell"] autorelease];          [cell addSubview:[[UILabel alloc] initWithFrame:CGRectMake(20.0f, 10.0f, 280.0f, 330.0f)]];      }      UILabel *sv = [[cell subviews] lastObject];      sv.text =  @"When in the Course of human events, it becomes necessary for one people to dissolve the political bands which have connected them with another, and to assume among the powers of the earth, the separate and equal station to which the Laws of Nature and of Nature's God entitle them, a decent respect to the opinions of mankind requires that they should declare the causes which impel them to the separation.";      sv.textAlignment = UITextAlignmentCenter;      sv.lineBreakMode = UILineBreakModeWordWrap;      sv.numberOfLines = 9999;      return cell;  }  

...to size the "sv" UILabel subview and the "cell" UITableViewCell to be sized just big enough to fit the text (and work with more or less text, and other types of text alignment)?  I looked at the UILabel textRectForBounds:limitedToNumberOfLines: method, but the documentation states that it should not be called directly (and should only be overridden).  I experimented with the UIView sizeToFit method, without success.

Update: I asked a new question about my problem with the NSString -sizeWithFont:forWidth:lineBreakMode: method.

like image 390
Daryl Spitzer Avatar asked Jan 02 '09 07:01

Daryl Spitzer


People also ask

How do you text the height of UILabel?

text = text; label. numberOfLines = 0; [label sizeToFit]; return cell; Also use NSString 's sizeWithFont:constrainedToSize:lineBreakMode: method to compute the text's height. Show activity on this post.

How to increase label size according to text in iOS?

Just use auto layout to add constraints to pin the left and top sides of the label. After that it will automatically resize.

How do you change the width of a UILabel?

To change the font or the size of a UILabel in a Storyboard or . XIB file, open it in the interface builder. Select the label and then open up the Attribute Inspector (CMD + Option + 5). Select the button on the font box and then you can change your text size or font.


2 Answers

I had to do this enough that I extended UILabel to do it for me:

@interface UILabel (BPExtensions) - (void)sizeToFitFixedWidth:(CGFloat)fixedWidth; @end  @implementation UILabel (BPExtensions)   - (void)sizeToFitFixedWidth:(CGFloat)fixedWidth {     self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, fixedWidth, 0);     self.lineBreakMode = NSLineBreakByWordWrapping;     self.numberOfLines = 0;     [self sizeToFit]; } @end 

then to have a label to have a variable multiline height but a fixed width just:

[myLabel sizeToFitFixedWidth:kSomeFixedWidth];

like image 85
BadPirate Avatar answered Sep 19 '22 06:09

BadPirate


You should use NSString's -sizeWithFont:forWidth:lineBreakMode: method to retrieve the associated sizing metrics for your label.

like image 41
Lily Ballard Avatar answered Sep 22 '22 06:09

Lily Ballard



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!