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

天大地大妈咪最大 提交于 2019-12-20 03:23:56

问题


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.


回答1:


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)



回答2:


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



来源:https://stackoverflow.com/questions/46176240/how-can-we-keep-a-text-fields-font-size-relative-to-the-screen-size

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!