当您在单击UITextfield后开始键入时,如何使第一个字母不自动大写?
当您在单击UITextfield后开始键入时,如何使第一个字母不自动大写?
在Objective-C中:
textField.autocapitalizationType = UITextAutocapitalizationTypeNone;
在Swift中:textField.autocapitalizationType = .none
对于SwiftUI,
TextField("I want entered text to be all lowercase", text:$myText)
.autocapitalization(.none)
您可以通过UITextInputTraits协议中的.autocapitalizationType
属性关闭自动大写。
textfield.autocapitalizationType = UITextAutocapitalizationTypeNone;
您可以在XIB(界面生成器)中的Text Field属性的Text Input Traits中设置TextField的大小写规则。
在UITextField中设置setAutocapitalizationType:UITextAutocapitalizationTypeNone。
在Swift中,您可以使用autocapitalizationType
属性:
yourTextField.autocapitalizationType = .none
在Swift中:
textField.autocapitalizationType = UITextAutocapitalizationType.None
textField.autocapitalizationType = .None
- Matias ElorriagatextField.autocapitalizationType = .none;
textfield.autocorrectionType = .no;
并且
textField.spellCheckingType = .no
只设置 .autocapitalizationType = .none; 是可行的,但最好同时设置另外两个属性来避免自动更正和拼写检查的大写。
尝试这段代码:
textfieldname.autocapitalizationType = UITextAutocapitalizationTypeNone;
当您在目标文本字段中输入任何内容时,此代码将使所有文本字段输入变为小写。
func textField(_ textFieldToChange: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
//just change this charectar username it's a text field
if textFieldToChange == username {
let characterSetNotAllowed = CharacterSet.whitespaces
if let _ = string.rangeOfCharacter(from:NSCharacterSet.uppercaseLetters) {
return false
}
if let _ = string.rangeOfCharacter(from: characterSetNotAllowed, options: .caseInsensitive) {
return false
} else {
return true
}
}
return true
}