Reputation: 12659
There are many solutions to this questions arround but couldn't find non-deprecated one.
I have an UILabel
with mode WordWrap and fixed width of, let's say 250. Lines are set to 0.
Here is what I tried:
UILabel *contentLabel = (UILabel*)[contentView viewWithTag:10];
CGSize size = [contentLabel.text sizeWithFont:contentLabel.font forWidth:contentLabel.frame.size.width lineBreakMode:NSLineBreakByWordWrapping];
NSLog(@"Label's height is: %d", size.height);
The output of height param is always 20 (so it's like one line), while te text is like 30 lines long.
I need that for UIScrollView purposes.
Upvotes: 8
Views: 25944
Reputation: 2160
This has worked for me:
*Set the numberOfLines property of UILabel to 0 *add this line: yourLabel.sizeToFit() after assigning text to the UILabel
Upvotes: 0
Reputation: 231
let l = UILabel()
l.numberOfLines = 0
l.layer.frame.size.width = self.view.frame.width - 40 /*(Padding 20 + 20)*/
l.font = UIFont(name: "BwModelica-Bold", size: 16.0)
l.text = "Your Any length Text!!"
let noOfLines = ceil(l.intrinsicContentSize.width) / l.frame.width
let lbl_height = noOfLines * l.intrinsicContentSize.height
This will be your Exact dynamic height of Label. Happy coding!!!
Upvotes: 0
Reputation: 856
which will return number of lines
- (int)lineCountForLabel:(UILabel *)label
{
CGFloat labelWidth = label.frame.size.width;
int lineCount = 0;
CGSize textSize = CGSizeMake(labelWidth, MAXFLOAT);
long rHeight = lroundf([label sizeThatFits:textSize].height);
long charSize = lroundf(label.font.leading);
lineCount = (int)( rHeight / charSize );
return lineCount;
}
by calling
[self lineCountForLabel:YOUR_LABEL];
Upvotes: 1
Reputation: 833
I was having trouble using boundingRectWithSize directly on my UILabel's attributedText — it was not accounting for the wrap to multiple lines, (the returned height was always 17.5). To work around this, I had to use boundingRectWithSize on the UILabel's text property and pass in the attributes dictionary separately (and not via [self.myLabel.attributedText attributesAtIndex:0 effectiveRange:nil]
).
CGRect labelSize = CGRectIntegral([self.myLabel.text
boundingRectWithSize:CGSizeMake(self.myLabel.frame.size.width, MAXFLOAT)
options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading
attributes:@{NSFontAttributeName:self.myLabel.font,
NSParagraphStyleAttributeName:paragraphStyle} context:nil]);
Upvotes: 4
Reputation: 1405
Use suggested in documentation method :
- (CGRect)boundingRectWithSize:(CGSize)size options:(NSStringDrawingOptions)options attributes:(NSDictionary *)attributes context:(NSStringDrawingContext *)context NS_AVAILABLE_IOS(7_0);
E.g.
CGSize maxSize = CGSizeMake(self.label.frame.size.width, MAXFLOAT);
CGRect labelRect = [self.label.text boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:self.label.font} context:nil];
NSLog(@"size %@", NSStringFromCGSize(labelRect.size));
Upvotes: 55
Reputation: 712
Try this out
//Here tweetText is an object of NSString and assign a text to it
NSString *tweetText = tweet.tweetText;
CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f);
CGSize size = [tweetText sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:NSLineBreakByWordWrapping];
//detailLabel is an object of UILabel
[detailLabel setText:tweetText];
//Set frame
[detailLabel setFrame:CGRectMake(76,20,280, MAX(size.height, 30.0f))];
Upvotes: -3
Reputation: 1223
Here is method which I'm using:
CGSize maximumSize = CGSizeMake(contentLabel.frame.size.width, 9999);
CGSize myStringSize = [eventName sizeWithFont:contentLabel.font
constrainedToSize:maximumSize
lineBreakMode:UILineBreakModeWordWrap];
Upvotes: -6