IOS how to determine the size of a UILabel dynamically

I am trying to figure out a way to find out the size of the UILabel base for the size of the string and font, in order to find out how to determine the size of the CGRect.

Here is what I know so far:

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

The problem is that I don't have a list of possible lines, but I need to center the label, and the line should not be interrupted (using the bite example above: "This is str ..."). Do any of you know how I can determine the size of the UILabel base for a string and font size?

I will be very grateful for your help.

+3
source share
7 answers

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 this helps

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

+5
source

. UILabel UITextView , , , , . .

, : UILabel, , UIKit , UITextView Text Kit ( iOS 7). , UIKit.

UILabel :

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

numberOfLines = 0. .

4000 , 100 , UILabel I, .

, UITextView, NSLayoutManager. (~ 50 , UITextView).

- .

+1

, , , . , ...

myLabel.lineBreakMode = NSLineBreakByWordWrapping;
myLabel.numberOfLines = 0;
0

UILabel .

, , .

0

You can calculate the widthlabels by doing the following:

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

Otherwise, you can simply set the line in the label and then call sizeToFit, it will set the framelabels based on the text.

0
source

in iOS7 use the method NSString boundingRectWithSize:options:context:. Check out the docs here .

CGRect rectYouNeed =
  [myLabel.text boundingRectWithSize:CGSizeMake(100, CGFLOAT_MAX)
                             options:NSStringDrawingUsesLineFragmentOrigin
                             attributes:@{ NSFontAttributeName : myLabel.font }
                             context:nil];
0
source

check my code, I wrote it in one of my applications.

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];
0
source

All Articles