UILabel text to fill in the frame width
Asked Answered
S

2

6

I want to build typography poster using UILabel

  • left and margin: 25
  • 320(device width) - 50(sum of the margins) = 270(label width frame)

The font size of each label should change in order to fit in 270 frame width

I try with sizeToFit(),adjustsFontSizeToFitWidth=true

var margin = 0;
let label = UILabel(frame: CGRectMake(25 , 72, 270, 70));
label.backgroundColor = UIColor.clearColor();
label.textAlignment = NSTextAlignment.Left;
label.textColor = UIColor.blackColor();
label.numberOfLines = 1;
label.font = UIFont.systemFontOfSize(50.0);
label.text = "Some Text";
label.adjustsFontSizeToFitWidth = true;
self.view.addSubview(label);
margin += 60;

let label2 = UILabel(frame: CGRectMake(25 , CGFloat(72+margin), 270, 70));
label2.backgroundColor = UIColor.clearColor();
label2.textAlignment = NSTextAlignment.Left;
label2.textColor = UIColor.whiteColor();
label2.numberOfLines = 1;
label2.font = UIFont.boldSystemFontOfSize(45.0);
label2.text = "Some Text Longer";
self.view.addSubview(label2);

Screenshot when lable1 and label2 adjustsFontSizeToFitWidth=true

enter image description here

The text should start from the end of first grey border and end in the beginning at the start of the second grey border

Sasaki answered 16/1, 2015 at 11:40 Comment(5)
The size of the font will change according to frame when adjustsFontSizeToFitWidth=true. Isn't it working for you? You have not set it for label2Otten
No adjustsFontSizeToFitWidth don't work. The text have a left padding and dont star the at 25 px. Also println(label.font); font-family: ".HelveticaNeueInterface-Regular"; font-weight: normal; font-style: normal; font-size: 50.00ptClouse
does the text in label get clipped? what is the problem with fontsize being 50 if it fits? You have set it as 50 for labelOtten
img - i.sstatic.net/k6Z35.png I want the text to start from first gray and end the second grey borderClouse
@EvenJohnson can't you make the font larger for that?Otten
O
6

adjustsFontSizeToFitWidth does not increase the size of the font in the label to fit the width.

According to Apple's documentation,

adjustsFontSizeToFitWidth is a Boolean value indicating whether the font size should be reduced in order to fit the title string into the label’s bounding rectangle.

Normally, the label text is drawn with the font you specify in the font property. If this property is set to YES, however, and the text in the text property exceeds the label’s bounding rectangle, the receiver starts reducing the font size until the string fits or the minimum font size is reached. In iOS 6 and earlier, this property is effective only when the numberOfLines property is set to 1.

You can try increasing the size of the font to fit the width of the frame.

Source : https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIView_Class/index.html#//apple_ref/occ/instm/UIView/sizeToFit

You can also use the following method to iteratively find the maximum size that fits the label.

How to adjust font size of label to fit the rectangle?

Otten answered 16/1, 2015 at 12:59 Comment(2)
label.font = UIFont.systemFontOfSize(30.0); label.text = "Some Text" label.sizeThatFits(CGSizeMake(270, 40)); label.sizeToFit(); label.adjustsFontSizeToFitWidth = true; img - imageupload.co.uk/images/2015/01/16/…Sasaki
I did not understand your comment. Can you please explain?Otten
S
4

This should just work without changing many defaults. The posted code doesn't set adjustsFontSizeToFitWidth, look like this:

label.adjustsFontSizeToFitWidth = true
Saleh answered 14/4, 2015 at 6:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.