6

I'm trying to figure out a way to know what would be the size of the UILabel base on the string and font size to know how define the CGRect dimensions.

Here what I have until know:

UILabel *myLabel = [[UILabel alloc] init];
myLabel.font = [UIFont fontWithName:@"BentonSansComp-Medium" size:50.0];
myLabel.text = @"This is a string example"  

The problem is I don't have a list of the posible strings but and need to center the label and the string should not be chop (using the sting above example: "This is a str...."). any of you knows how can I determine the size of the UILabel base on the string and font size?

I'll really appreciate your help.

user2924482
  • 8,380
  • 23
  • 89
  • 173

6 Answers6

7

just use

 UILabel *myLabel = [[UILabel alloc] init];
 myLabel.font = [UIFont fontWithName:@"BentonSansComp-Medium" size:50.0];
 myLabel.text = @"This is a string example" 
 [myLabel sizeToFit]; //it will resize the label to just the text is set
 CGRectMake size = myLabel.frame; // u will get the frame of the label once is resized
 float height = size.size.height;  //to get the height of the label
 float width = size.size.width;  //to get the width of the label

hope it helps

Make sure evrytime you change the text u call the [myLabel sizeToFit]; method

Calleth 'Zion'
  • 613
  • 3
  • 15
1

Just had the same problem. Calculating a lot of sizes of UILabels or UITextViews is expensive, so I wondered if there's a cheaper way than configuring a label or text view and then asking it for the size. This is especially important when calculating row heights for long table views.

Turns out, there is: UILabel seems to be built up UIKit's string additions for layout and drawing while UITextView uses Text Kit (in iOS 7). Both facilities can be configured to use the same settings and calculate string dimensions cheaper that using the UIKit classes.

For a UILabel here's what I found out:

CGFloat labelHeight = [testString boundingRectWithSize:(CGSize){ labelWidth, CGFLOAT_MAX }
                                               options:NSStringDrawingUsesLineFragmentOrigin
                                            attributes:@{ NSFontAttributeName : labelFont };
                                               context:nil].size.height;
labelHeight = ceil(labelHeight);

This assumes numberOfLines = 0. Note the need to round up the result.

I tested above code with about 4000 string rendered into 100 different widths and all results were equal to the UILabel I used for comparison.

I did the same to calculate dimensions of UITextView which is more complicated and required to set up an NSLayoutManager. Here the speedup is even more impressive (~ 50 times faster than using the UITextView).

If anybody's interested here's the code and test project.

Nikolai Ruhe
  • 81,520
  • 17
  • 180
  • 200
0

I think what you need is multiple lines, that will add up the area needed below. Follow this and there won't be ... anymore

myLabel.lineBreakMode = NSLineBreakByWordWrapping;
myLabel.numberOfLines = 0;
isklikas
  • 190
  • 2
  • 16
0

You can calculate the width of the label by doing this:

[@"STRING" sizeWithFont:[UIFont fontWithName:@"FONT" size:12] forWidth:200 lineBreakMode:NSLineBreakByWordWrapping];

Otherwise you can just set the string in the label, and then call sizeToFit, this will set the frame of the label based on the text.

Unheilig
  • 16,196
  • 193
  • 68
  • 98
Darkslave
  • 111
  • 5
  • - (CGSize)sizeWithFont:(UIFont *)font forWidth:(CGFloat)width lineBreakMode:(NSLineBreakMode)lineBreakMode NS_DEPRECATED_IOS(2_0, 7_0, "Use -boundingRectWithSize:options:attributes:context:"); – user2924482 Feb 06 '14 at 19:21
0

in iOS7 use NSString method boundingRectWithSize:options:context:. Look docs here.

CGRect rectYouNeed =
  [myLabel.text boundingRectWithSize:CGSizeMake(100, CGFLOAT_MAX)
                             options:NSStringDrawingUsesLineFragmentOrigin
                             attributes:@{ NSFontAttributeName : myLabel.font }
                             context:nil];
Valentin Shamardin
  • 3,569
  • 4
  • 34
  • 51
0

check my code , i have written it in one of my app.

CGSize maximumLabelSize = CGSizeMake(270, FLT_MAX);
CGSize expectedLabelSize = [myString sizeWithFont:[UIFont fontWithName:@"Helvetica" size:16.0f] constrainedToSize:maximumLabelSize lineBreakMode:NSLineBreakByWordWrapping];

NSLog(@"Expected Size of label based on string--%@",NSStringFromCGSize(expectedLabelSize));
//adjust the label the the new height.
CGRect newFrame = myLabel.frame;//get initial frame of your label
newFrame.size.height = expectedLabelSize.height;
myLabel.frame = newFrame;
NSLog(@"new frame of label--%@",NSStringFromCGRect(newFrame));
[myLabel setText:attString];
Pawan Rai
  • 3,434
  • 4
  • 32
  • 42
  • sizeWithFont is deprecated, the boundingRectWithSize:options:attributes:context method is the updated replacement. Cheers – Jim Tierney Feb 06 '14 at 19:44
  • thanks , this code is little bit old. i have written it around 1 year ago for updating label size. – Pawan Rai Feb 06 '14 at 19:49