Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculating UILabel Text Size

I am drawing UILabels programmatically. They get their sizes from a database. So I cannot just use sizeToFit. I have already implemented a function that redraws UILabels with a passed ratio. So all I need to find is the text in UILabel from my view that would require the maximum ratio to redraw UILabels. So finally I need to do something like this:

    double ratio = 1.00;     for (UILabel* labels in sec.subviews) {          float widthLabel = labels.frame.size.width;         float heightLabel = labels.frame.size.height;         float heightText = //get the text height here         float widthText = //get the text width here         if (widthLabel < widthText) {             ratio = MAX(widthText/widthLabel,ratio);         }         if (heightLabel < heightText) {             ratio = MAX(heightText/heightLabel, ratio);         }     }     //redraw UILabels with the given ratio here 

So how can I get the height and width size of a text, as some of my text do not fit into the label I cannot simply use label bounds? I am using Xcode 5 and iOS 7.

like image 657
Sarp Kaya Avatar asked Oct 02 '13 01:10

Sarp Kaya


People also ask

How do you text the height of UILabel?

text = text; label. numberOfLines = 0; [label sizeToFit]; return cell; Also use NSString 's sizeWithFont:constrainedToSize:lineBreakMode: method to compute the text's height. Show activity on this post.

How is font width calculated?

The calculation of font size as prescribed in DIN 1450 is based on x-heights. Font size (here x-height) is generally set depending on the viewer's distance from it. The shorter a viewer's distance from a text is, the smaller its font can be and vice-versa.


2 Answers

All of the [NSString sizeWithFont...] methods are deprecated in iOS 7. Use this instead.

CGRect labelRect = [text                     boundingRectWithSize:labelSize                     options:NSStringDrawingUsesLineFragmentOrigin                     attributes:@{                      NSFontAttributeName : [UIFont systemFontOfSize:14]                     }                     context:nil]; 

Also see https://developer.apple.com/documentation/foundation/nsstring/1619914-sizewithfont.

UPDATE - example of boundingRectWithSize output

Per your comment I did a simple test. The code and output is below.

// code to generate a bounding rect for text at various font sizes NSString *text = @"This is a long sentence. Wonder how much space is needed?"; for (NSNumber *n in @[@(12.0f), @(14.0f), @(18.0f)]) {     CGFloat fontSize = [n floatValue];     CGRect r = [text boundingRectWithSize:CGSizeMake(200, 0)                                   options:NSStringDrawingUsesLineFragmentOrigin                                attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:fontSize]}                                   context:nil];     NSLog(@"fontSize = %f\tbounds = (%f x %f)",           fontSize,           r.size.width,           r.size.height); } 

this produces the following output (note that the bounds change as expected as the font size gets larger):

fontSize = 12.000000    bounds = (181.152008 x 28.632000) fontSize = 14.000000    bounds = (182.251999 x 50.105999) fontSize = 18.000000    bounds = (194.039993 x 64.421997) 
like image 164
XJones Avatar answered Oct 16 '22 10:10

XJones


Length gets the number of characters. If you want to get the width of the text:

Objective-C

CGSize textSize = [label.text sizeWithAttributes:@{NSFontAttributeName:[label font]}]; 

Swift 4

let size = label.text?.size(withAttributes: [.font: label.font]) ?? .zero 

This gets you the size. And you can compare the textSize.width of each label.

like image 33
H. Serdar Çınar Avatar answered Oct 16 '22 11:10

H. Serdar Çınar