gpt4 book ai didi

ios - 强制小写 - ios swift

转载 作者:搜寻专家 更新时间:2023-10-30 22:24:40 30 4
gpt4 key购买 nike

我想在用户输入时强制在 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
}
}

最佳答案

首先,您应该在文本字段上设置以下属性以限制自动大写:

textfield.autocapitalizationType = UITextAutocapitalizationType.None

这是进一步限制它的方法:

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
}

针对 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
}

关于ios - 强制小写 - ios swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33195946/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com