UITextField 只允许字母和 space 并限制最大字符数
UITextField only allow letters and space and limit maximum characters
在文本字段中,我只想允许字母和 spaces。以下解决方案似乎有效,但它有几个问题
1) 不允许space
2) 它在输入时计算数字,并包括在不正确的最大限制的总计数中。我们应该只计算我们在现场看到的东西
我一直在寻找解决方案,但其中大多数似乎已经过时或不适合我的目的。
有帮助吗?
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
// Don't allow number in name field
if textField == nameTextField && string.count > 0 {
let textString = (textField.text! as NSString).replacingCharacters(in: range, with: string)
let LettersOnly = NSCharacterSet.letters
let strValid = LettersOnly.contains(UnicodeScalar.init(string)!)
return strValid && textString.count <= 30
}
return true
}
如果我理解正确,您可以使用如下内容:
class ViewController: UIViewController, UITextFieldDelegate {
let allowedCharacters = CharacterSet.letters.union(CharacterSet(charactersIn: " "))
let maxLength = 10
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
let newText = (textField.text! as NSString).replacingCharacters(in: range, with: string)
// check characters
guard newText.rangeOfCharacter(from: allowedCharacters.inverted) == nil else { return false }
// check length
guard newText.count <= maxLength else { return false }
return true
}
}
或更短:
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
let newText = (textField.text! as NSString).replacingCharacters(in: range, with: string)
return (newText.rangeOfCharacter(from: allowedCharacters.inverted) == nil) && (newText.count <= maxLength)
}
在文本字段中,我只想允许字母和 spaces。以下解决方案似乎有效,但它有几个问题
1) 不允许space
2) 它在输入时计算数字,并包括在不正确的最大限制的总计数中。我们应该只计算我们在现场看到的东西
我一直在寻找解决方案,但其中大多数似乎已经过时或不适合我的目的。 有帮助吗?
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
// Don't allow number in name field
if textField == nameTextField && string.count > 0 {
let textString = (textField.text! as NSString).replacingCharacters(in: range, with: string)
let LettersOnly = NSCharacterSet.letters
let strValid = LettersOnly.contains(UnicodeScalar.init(string)!)
return strValid && textString.count <= 30
}
return true
}
如果我理解正确,您可以使用如下内容:
class ViewController: UIViewController, UITextFieldDelegate {
let allowedCharacters = CharacterSet.letters.union(CharacterSet(charactersIn: " "))
let maxLength = 10
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
let newText = (textField.text! as NSString).replacingCharacters(in: range, with: string)
// check characters
guard newText.rangeOfCharacter(from: allowedCharacters.inverted) == nil else { return false }
// check length
guard newText.count <= maxLength else { return false }
return true
}
}
或更短:
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
let newText = (textField.text! as NSString).replacingCharacters(in: range, with: string)
return (newText.rangeOfCharacter(from: allowedCharacters.inverted) == nil) && (newText.count <= maxLength)
}