swift2 如何限制文本字段的输入长度- Swift 2

m528fe3b  于 2022-11-06  发布在  Swift
关注(0)|答案(5)|浏览(254)

这段代码运行得很好,我不能键入除整数以外的任何内容,即使我试图粘贴它。
我想再加一个优化,就是限制输入的长度。下面是我的代码:

func initializeTextFields()
{
APTeams.delegate = self
APTeams.keyboardType = UIKeyboardType.NumberPad

APRounds.delegate = self
APRounds.keyboardType = UIKeyboardType.NumberPad

APBreakers.delegate = self
APBreakers.keyboardType = UIKeyboardType.NumberPad

}

func textField(textField: UITextField, shouldChangeCharactersInRange range:     NSRange, replacementString string: String) -> Bool {

// Find out what the text field will be after adding the current edit
let text = (textField.text! as NSString).stringByReplacingCharactersInRange(range, withString: string)

if  text == "" {
    return true
}

if let _ = Int(text) {
    return true
}

else {
    return false
}

}

要实现这一点,我需要添加什么呢?所有TextField的最大输入长度应该〈= 4。
顺便说一句,所有的代码都在Swift 2中。从我在尝试实现我以前问过的问题的答案时遇到的问题来看,我发现有些方法是不同的。

nwsw7zdq

nwsw7zdq1#

count(textField.text)在SWIFT 2.0中已弃用

public func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {

        if let textField = textField as? UITextField {

            if (range.length + range.location > textField.text!.characters.count) {
                return false;
            }

            let newLength = textField.text!.characters.count + string.characters.count - range.length;
            switch(textField.tag) { //In case you want to handle multiple textfields
                case Constants.TAG1:
                    return newLength <= 20;
                case Constants.TAG2:
                    return newLength <= 30;
                default:
                    return newLength <= 15;
            }
        }
        return true;
    }
1wnzp6jl

1wnzp6jl2#

将textfield委派方法中的条件写成:-

func textField(textField: UITextField!, shouldChangeCharactersInRange range: NSRange, replacementString string: String!) -> Bool {

    if (count(textField.text) > 4  && range.length == 0)

   {
        return false // return NO to not change text
    }
    else

    {

     }

将所有代码部分写入其他部分。

vmpqdwk3

vmpqdwk33#

委派方法或NSFormatter,例如NSNumberFormatter。格式化程序通常是最合适的,因为它也提供当地语系化支援。

lp0sw83n

lp0sw83n4#

我知道它有点太晚了,但我仍然想分享它太,我发现了一种方法,这是更容易设置一个限制字符的文本字段在快速发展。
下面是代码:-

import UIKit

private var maxLengths = [UITextField: Int]()

extension UITextField {

    @IBInspectable var maxLength: Int {
        get {
            guard let length = maxLengths[self] else {
                return Int.max
            }

            return length
        }
        set {
            maxLengths[self] = newValue
            addTarget(self, action: #selector(limitLength), for: .editingChanged)
        }
    }

    @objc func limitLength(textField: UITextField) {
        guard let prospectiveText = textField.text, prospectiveText.count > maxLength else {
            return
        }

        let selection = selectedTextRange
        let maxCharIndex = prospectiveText.index(prospectiveText.startIndex, offsetBy: maxLength)

        #if swift(>=4.0)
            text = String(prospectiveText[..<maxCharIndex])
        #else
            text = prospectiveText.substring(to: maxCharIndex)
        #endif

        selectedTextRange = selection
    }

}

然后通过面板设置限制。
图片:x1c 0d1x

yks3o0rb

yks3o0rb5#

只要尝试这个限制TF的长度

编辑已更改的TF操作出口

@IBAction func otpTF2EditingChnaged(_ sender: UITextField) {
        if (sender.text?.count == 1) {
            otpTF3.becomeFirstResponder()
        }
        checkMaxLength(textField: sender , maxLength: 1)
    }

将限制长度的函数

private func checkMaxLength(textField: UITextField!, maxLength: Int) {
        if (textField.text!.count > maxLength) {
            textField.deleteBackward()
        }
    }

相关问题