当我尝试How to you set the maximum number of characters that can be entered into a UITextField using swift?时,我看到如果我使用全部10个字符,我也无法删除该字符。
我唯一能做的就是取消操作(一起删除所有字符)。
有谁知道如何不阻止键盘(所以我不能添加其他字母/符号/数字,但我可以使用退格键)?
使用Swift 5和iOS 12,尝试以下textField(_:shouldChangeCharactersIn:replacementString:)
方法的实现,该方法是UITextFieldDelegate
协议的一部分:
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
guard let textFieldText = textField.text,
let rangeOfTextToReplace = Range(range, in: textFieldText) else {
return false
}
let substringToReplace = textFieldText[rangeOfTextToReplace]
let count = textFieldText.count - substringToReplace.count + string.count
return count <= 10
}
range
(NSRange
)到rangeOfTextToReplace
(Range<String.Index>
)的转换。请参阅此video tutorial以了解为何此转换很重要。textField
的smartInsertDeleteType
值设置为UITextSmartInsertDeleteType.no
。这将防止在执行粘贴操作时可能插入(不需要的)额外空间。下面的完整示例代码显示了如何在textField(_:shouldChangeCharactersIn:replacementString:)
中实现UIViewController
:
import UIKit
class ViewController: UIViewController, UITextFieldDelegate {
@IBOutlet var textField: UITextField! // Link this to a UITextField in Storyboard
override func viewDidLoad() {
super.viewDidLoad()
textField.smartInsertDeleteType = UITextSmartInsertDeleteType.no
textField.delegate = self
}
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
guard let textFieldText = textField.text,
let rangeOfTextToReplace = Range(range, in: textFieldText) else {
return false
}
let substringToReplace = textFieldText[rangeOfTextToReplace]
let count = textFieldText.count - substringToReplace.count + string.count
return count <= 10
}
}
请注意本文中提到的UITextField的撤消错误:Set the maximum character length of a UITextField
这是你如何在swift中修复它
if(range.length + range.location > count(textField.text)) {
return false;
}
Here is my version of code. Hope it helps!
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
let invalidCharacters = NSCharacterSet(charactersInString: "0123456789").invertedSet
if let range = string.rangeOfCharacterFromSet(invalidCharacters, options: nil, range:Range<String.Index>(start: string.startIndex, end: string.endIndex))
{
return false
}
if (count(textField.text) > 10 && range.length == 0)
{
self.view.makeToast(message: "Amount entry is limited to ten digits", duration: 0.5, position: HRToastPositionCenter)
return false
}
else
{
}
return true
}
我一直在我的一个应用程序中使用这个协议/扩展,它更具可读性。我喜欢它如何识别退格并明确地告诉你何时一个角色是退格。
有些事情需要考虑:
1.无论实现什么,此协议扩展都需要指定字符限制。这通常是您的ViewController,但您可以将字符限制实现为计算属性并返回其他内容,例如您的某个模型上的字符限制。
2.您需要在文本字段的shouldChangeCharactersInRange委托方法中调用此方法。否则,您将无法通过返回false等来阻止文本输入。
3.您可能希望允许退格字符通过。这就是我添加额外功能来检测退格的原因。你的shouldChangeCharacters方法可以检查这个并尽早返回'true',这样你总是允许退格。
protocol TextEntryCharacterLimited{
var characterLimit:Int { get }
}
extension TextEntryCharacterLimited{
func charactersInTextField(textField:UITextField, willNotExceedCharacterLimitWithReplacementString string:String, range:NSRange) -> Bool{
let startingLength = textField.text?.characters.count ?? 0
let lengthToAdd = string.characters.count
let lengthToReplace = range.length
let newLength = startingLength + lengthToAdd - lengthToReplace
return newLength <= characterLimit
}
func stringIsBackspaceWith(string:String, inRange range:NSRange) -> Bool{
if range.length == 1 && string.characters.count == 0 { return true }
return false
}
}
如果你们有兴趣,我有一个Github回购,我已经采取了一些这个字符限制行为并放入iOS框架。你可以实现一个协议来获得类似Twitter的角色限制显示,显示你超出角色限制的程度。
由于委托是一对一的关系,我可能想出于其他原因在其他地方使用它,我喜欢限制文本字段长度在其设置中添加此代码:
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)!
setup()
}
required override init(frame: CGRect) {
super.init(frame: frame)
setup()
}
func setup() {
// your setup...
setMaxLength()
}
let maxLength = 10
private func setMaxLength() {
addTarget(self, action: #selector(textfieldChanged(_:)), for: UIControlEvents.editingChanged)
}
@objc private func textfieldChanged(_ textField: UITextField) {
guard let text = text else { return }
let trimmed = text.characters.prefix(maxLength)
self.text = String(trimmed)
}
您需要检查现有字符串加上输入是否大于10。
func textField(textField: UITextField!,shouldChangeCharactersInRange range: NSRange, replacementString string: String!) -> Bool {
NSUInteger newLength = textField.text.length + string.length - range.length;
return !(newLength > 10)
}
我是这样做的:
func checkMaxLength(textField: UITextField!, maxLength: Int) {
if (countElements(textField.text!) > maxLength) {
textField.deleteBackward()
}
}
该代码适合我。但我使用故事板。在故事板中,我在视图控制器中为编辑更改添加了文本字段的操作。
Swift 4的更新
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
guard let text = textField.text else { return true }
let newLength = text.count + string.count - range.length
return newLength <= 10
}
从@Martin回答中添加更多细节
// linked your button here
@IBAction func mobileTFChanged(sender: AnyObject) {
checkMaxLength(sender as! UITextField, maxLength: 10)
}
// linked your button here
@IBAction func citizenTFChanged(sender: AnyObject) {
checkMaxLength(sender as! UITextField, maxLength: 13)
}
func checkMaxLength(textField: UITextField!, maxLength: Int) {
// swift 1.0
//if (count(textField.text!) > maxLength) {
// textField.deleteBackward()
//}
// swift 2.0
if (textField.text!.characters.count > maxLength) {
textField.deleteBackward()
}
}
func checkMaxLength(textField: UITextField!, maxLength: Int) {
if (textField.text!.characters.count > maxLength) {
textField.deleteBackward()
}
}
IOS 9的一个小改动
在Swift 4中
10个字符限制文本字段并允许删除(退格)
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
if textField == userNameFTF{
let char = string.cString(using: String.Encoding.utf8)
let isBackSpace = strcmp(char, "\\b")
if isBackSpace == -92 {
return true
}
return textField.text!.count <= 9
}
return true
}
斯威夫特3
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
let nsString = NSString(string: textField.text!)
let newText = nsString.replacingCharacters(in: range, with: string)
return newText.characters.count <= limitCount
}
我使用IBInspectable
发布了一个解决方案,因此您可以在界面生成器中或以编程方式更改最大长度值。 Check it out here
如果你想覆盖最后一个字母:
let maxLength = 10
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
if range.location > maxLength - 1 {
textField.text?.removeLast()
}
return true
}