Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I sizeToFit a UILabel by changing only the height and not the width?

[self.Review sizeToFit]; 

Result before sizeToFit:

NSStringFromCGRect(self.Review.frame): {{90, 20}, {198, 63}} 

Result After sizeToFit:

NSStringFromCGRect(self.Review.frame): {{90, 20}, {181, 45}} 

I want the width to remain the same. I just want to change the height. THe automask is

(lldb) po self.Review (UILabel *) $1 = 0x08bb0fe0 <UILabel: 0x8bb0fe0; frame = (90 20; 181 45); text = 'I'm at Mal Taman Anggrek ...'; clipsToBounds = YES; opaque = NO; autoresize = LM+RM+H; userInteractionEnabled = NO; layer = <CALayer: 0x8bb68b0>> 

I know that there is a way to do so with: How to adjust and make the width of a UILabel to fit the text size?

The answers are either strange (we need to resupply the font information). Or unclear.

You will also need to define a maximum width, and tell your program what to do if sizeToFit gives you a width greater than that maximum.

I will use the strange solution of using sizeWithFont. It's strange because UILabel already knows the font in the label.

Actually how does sizeToFit behave anyway? How does it decide whether we need thinner or taller UILabel?

like image 884
Anonymous White Avatar asked Oct 25 '12 11:10

Anonymous White


People also ask

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.

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 do I resize a label in Xcode?

Here's the step that work in mine: Set width constraint to the label, then click the constraint. Select Size Inspector. Set the relation to less than or equal, and set max width.


2 Answers

You can achieve the same result with sizeThatFits.

CGSize size = [label sizeThatFits:CGSizeMake(label.frame.size.width, CGFLOAT_MAX)]; CGRect frame = label.frame; frame.size.height = size.height; label.frame = frame; 

Or alternatively, with sizeToFit.

CGRect frame = label.frame; [label sizeToFit]; frame.size.height = label.frame.size.height; label.frame = frame; 
like image 57
vadimtrifonov Avatar answered Sep 23 '22 08:09

vadimtrifonov


This is how it is done. Because the label already contains the font information, including it in this method call is trivial.

CGSize size = [label.text sizeWithFont:label.font                      constrainedToSize:CGSizeMake(maxWidth, MAXFLOAT)                          lineBreakMode:UILineBreakModeWordWrap];  CGRect labelFrame = label.frame; labelFrame.size.height = size.height; label.frame = labelFrame; 

Swift version using the more up-to-date boundingRectWithSize:

let maxHeight = CGFloat.infinity let rect = label.attributedText?.boundingRectWithSize(CGSizeMake(maxWidth, maxHeight),         options: .UsesLineFragmentOrigin, context: nil) var frame = label.frame frame.size.height = rect.size.height label.frame = frame 
like image 35
Mundi Avatar answered Sep 25 '22 08:09

Mundi