Dynamic UILabel line count calculation (iOS7)

There are many solutions for this arround issue, but non-debrecated could not be found.

I have a UILabel with UILabel mode and a fixed width, say 250. The 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 height is: %d", size.height); 

The output of the height parameter is always 20 (so that it looks like a single line), and the text is like 30 lines.

I need this for UIScrollView purposes.

+8
ios uilabel ios7
Sep 13 '13 at 10:33
source share
7 answers

Use in the documentation method:

 - (CGRect)boundingRectWithSize:(CGSize)size options:(NSStringDrawingOptions)options attributes:(NSDictionary *)attributes context:(NSStringDrawingContext *)context NS_AVAILABLE_IOS(7_0); 

eg.

  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)); 
+55
Sep 13 '13 at 11:31 on
source share

I am having problems using boundingRectWithSize directly in my UILabel attribittedText - it did not account for multi-line wrapping (the return height was always 17.5). To get around this, I had to use boundingRectWithSize in the UILabel text property and pass in the attributes dictionary separately (and not through [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]); 
+3
Jan 29 '15 at 22:44
source share

You can use this simple method:

which will return the number of rows

 - (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; } 

causing

 [self lineCountForLabel:YOUR_LABEL]; 
+1
Jul 20 '15 at 10:49
source share
  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 cue height. Good coding !!!

0
Jan 18 '19 at 7:46
source share

UILineBreakModeWordWrap is deprecated, now you can use this

 CGSize nameSize = [content sizeWithFont:[UIFont fontWithName:@"verdana" size:bigFontSize] constrainedToSize:CGSizeMake(labelWidth, labelHeight) lineBreakMode:NSLineBreakByWordWrapping]; NSLog(@"width = %f, height = %f", nameSize.width, nameSize.height); 
-3
Sep 13 '13 at 10:59 on
source share

try it

 //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))]; 
-3
Sep 13 '13 at 11:01
source share

Here is the method I'm using:

 CGSize maximumSize = CGSizeMake(contentLabel.frame.size.width, 9999); CGSize myStringSize = [eventName sizeWithFont:contentLabel.font constrainedToSize:maximumSize lineBreakMode:UILineBreakModeWordWrap]; 
-6
Sep 13 '13 at 10:51 on
source share



All Articles