简体   繁体   中英

How can I adjust the font size according to the size of the text field?

I want a text field to fill in the users' name and the resizing works, but the font size does not change with it. How can I do this so the font size gets adjusted, for example, 160 for an iPad and 50 for the iPhone 5 automatically?

https://imgur.com/8nAQUGJ

https://imgur.com/00AA5B7

Set font size for wR hR 160. To handle font for specified device, you should catch iPhone device model.

在此输入图像描述

You should care about constraints if UITextField for iPad/iPhone in Storyboard.

Or use next code, for example, in func textFieldDidEndEditing(_ textField: UITextField) :

textField.adjustsFontSizeToFitWidth = true

Or here:

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {

     textField.adjustsFontSizeToFitWidth = true
     return true
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM