我在一个社交网络上工作,我希望用户名只使用小写,在username text字段中,我如何使键盘不允许使用大写字母(我可以强制textfield.text字母为小写,但我并不真的想这样做),我也需要它为UISearchBox工作,因为如果用户可以激活大写字母按钮,搜索小写用户就无法很好地工作(它在搜索中返回nil)
textField.autocapitalizationType = .None
也许设置所有三个属性的工作方式与我的this答案中所描述的类似https://stackoverflow.com/a/52489530/855261
还可以设置UITextField方法以避免键入大写字母。
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
if let _ = string.rangeOfCharacter(from: .uppercaseLetters) {
// Don't allow upper case letters
return false
}
return true
}
Swift 5带swiftUI
TextField(“Search”, text: $userName).autocapitalization(.none)