xcode 强制小写 - ios swift
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33195946/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Force lowercase - ios swift
提问by Abhinav
I would like to force lowercase in an UITextfield
when the user is typing.
I came out so far with this code, but seems like it's not lowering characters.
我想UITextfield
在用户输入时强制使用小写字母。到目前为止,我已经用这段代码出来了,但似乎并没有降低字符。
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
if string.characters.count == 0 {
return true
}
let currentText = textField.text ?? ""
let prospectiveText = (currentText as NSString).stringByReplacingCharactersInRange(range, withString: string.lowercaseString)
switch textField {
// Allow only lower-case vowels in this field,
// and limit its contents to a maximum of 6 characters.
case userNameTextField:
return prospectiveText.characters.count <= 27
default:
return true
}
}
回答by Abhinav
First you should set following property on your textfield to restrict auto capitalisation:
首先,您应该在文本字段上设置以下属性以限制自动大写:
textfield.autocapitalizationType = UITextAutocapitalizationType.None
And this is how you can restrict it further:
这就是您可以进一步限制它的方法:
func textField(textField: UITextField!, shouldChangeCharactersInRange range: NSRange, replacementString string: String!) -> Bool {
if let _ = string.rangeOfCharacterFromSet(NSCharacterSet.uppercaseLetterCharacterSet()) {
// Do not allow upper case letters
return false
}
return true
}
UPDATED FOR SWIFT 4
为 Swift 4 更新
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
if let _ = string.rangeOfCharacter(from: .uppercaseLetters) {
// Do not allow upper case letters
return false
}
return true
}
回答by tim_yng
You could do like this and lowercase the entire string when something has changed.
您可以这样做,并在发生变化时将整个字符串小写。
textfield.addTarget(self, action: "textViewChanged", forControlEvents: .EditingChanged);
func textViewChanged(){
textfield.text = textfield.text?.lowercaseString;
}
回答by Lobo
for swift 3 users, the above code given by Abhinav is just converted to the following
对于swift 3用户,Abhinav给出的上述代码只是转换为以下
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
if let _ = string.rangeOfCharacter(from:NSCharacterSet.uppercaseLetters) {
return false
}
return true
}
回答by Sajjad Sarkoobi
if you want to convert all input characters to lower case you should do this code:
如果要将所有输入字符转换为小写,则应执行以下代码:
Swift 4:
斯威夫特 4:
in override func viewDidLoad()
add this:
在override func viewDidLoad()
补充一点:
textfield.addTarget(self, action: #selector(self.textFieldDidChange(_:)), for: UIControlEvents.editingChanged)
and then add this function to your class:
然后将此函数添加到您的类中:
@objc func textFieldDidChange(_ textField: UITextField) {
if let text:String = textfield.text {
DispatchQueue.main.async {
self.textfield.text = text.lowercased()
}
}
}
it is necessary to change it in main thread.
有必要在主线程中更改它。