如何根据文本字段的大小调整字体大小?

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

我想要一个文本字段来填写用户名并且调整大小有效,但字体大小不会随之改变。我该怎么做才能自动调整字体大小,例如,iPad 为 160,iPhone 5 自动为 50?

https://imgur.com/8nAQUGJ

https://imgur.com/00AA5B7

设置 wR hR 160 的字体大小。要处理指定设备的字体,您应该捕获 iPhone 设备型号。

如果 UITextField 在 Storyboard 中 iPad/iPhone,你应该关心约束。

或使用下一个代码,例如 func textFieldDidEndEditing(_ textField: UITextField):

textField.adjustsFontSizeToFitWidth = true

或此处:

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

     textField.adjustsFontSizeToFitWidth = true
     return true
}