iNick
iNick

Reputation: 115

Increase text size with bigger devices swift

I am currently working on a project and want the project to look exactly the same with every screen size. I have successfully done it with everything except the text. For example here is what it looks like on an iPhone:

enter image description here

And as you can see on an iPad the text is smaller. (or remains the same size)

iPad:

enter image description here

I would like the text size on the iPad to increase with the size of the screen. Any help would be really appreciated thank you!

Upvotes: 0

Views: 1867

Answers (3)

Patonz
Patonz

Reputation: 925

Try the first run on the iphone 7 plus (the bigger resolution :D) with this code:

label?.font = self.titleLabel!.font.withSize(45)
label.titleLabel?.adjustsFontSizeToFitWidth = true
label.textLabel?.minimumScaleFactor = 0.5

So now, check the size of text and fix it. When finally decide the right font size, check this on the others emulators ( like iphone 6 and iphone 5). Now fix the scaleFactor, but i think 0.5 generally is enough

Upvotes: 0

hooliooo
hooliooo

Reputation: 548

Size Classes is your best bet.

Size classes let you set up view properties such as font size according to screen size.

Upvotes: 2

Sweeper
Sweeper

Reputation: 273135

Here's some guidance.

I would suggest you calculate the font size of the text in viewWillLayoutSubviews method.

You can call the boundingRect(with:options:attributes:context) method on a string to get a CGRect that can contain the text. Then compare this rect to the rect of the label. If it is too small, increase the font size by one and try again. Keep doing this until the text's rect is larger than the label's rect.

If you're lazy and you don't want to write the algorithm yourself, try FittableFontLabel.

Just create a fittable font label in the storyboard, stretch it to the size you want, add some constraints and the text's size will be calculated and set for you!

Upvotes: 0

Related Questions