Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can we keep a text field's font size relative to the screen size?

I have a text field and I have given it a regular font with 13 px in size.

But I want the font size to increment with respect to screen size.

Thanks.

like image 828
Shankar Naik Avatar asked Dec 24 '22 14:12

Shankar Naik


2 Answers

You can calculate the font size based on the screen's dimensions.

let baseWidth: CGFloat = 320 // the width of the screen where you want to use 13pt
let fontSize = 13 * (UIScreen.main.size.width / baseWidth)
let font = UIFont(name: "HelveticaNeue", size: fontSize)
like image 189
Tamás Sengel Avatar answered Dec 26 '22 02:12

Tamás Sengel


You can simply do this by checking, Automatically Adjusts Font in the Attributes Inspector Like this

like image 26
Austin Michael Avatar answered Dec 26 '22 03:12

Austin Michael