iOS how to determine the size of UILabel dynamically

ios, iphone, objective-c, uilabel

Solution

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

Problem

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.

Original source

Related problems