限制Swift 2中多个UITextField的长度

时间:2015-12-22 09:10:23

标签: ios swift uitextfield

限制TextField长度的解决方案,但函数count已更新,还有count()函数,所以我不明白如何使用它:

func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
    let newLength = count(textField.text.utf16) + count(string.utf16) - range.length
    return newLength <= 10 // Bool
}

如何更新此功能以处理多个TextField,我想我必须说

textField = thisTextField.text...

4 个答案:

答案 0 :(得分:5)

shouldChangeCharactersInRange执行以下操作(引自docs

  

询问代表是否应更改指定的文本。

您添加的此方法代码会检查它是否超出您的限制(在您的示例中,它是10)并返回false,这意味着textField不应更改值。如果它没有超过限制,它将返回true,textField将改变值。

要为多个textField执行此操作,您需要为多个textField提供插座,然后在此方法中使用简单的if语句来完成此任务。

@IBOutlet weak var textfield1: UITextField!
@IBOutlet weak var textfield2: UITextField!

func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
    let newLength = textField.text.characters.count + string.characters.count - range.length
    if textField == textField1 {
        return newLength <= 10 // Bool
    } else if textField == textField2 {
        return newLength <= 15 // Bool
    }

    return true
}

为了能够在代码中使用上述方法,包含这些textField的UIViewController将需要实现UITextFieldDelegate协议,然后设置UITextField的委托属性为UIViewController

同样关于count方法。它已经多次更新。要计算字符串的字符数:

在Swift1.2之前 - &gt; countElements(string)
Swift1.2 - &gt; count(string)
Swift2 - &gt; string.characters.count

答案 1 :(得分:1)

从这个回答:Set the maximum character length of a UITextField

虽然UITextField类没有max length属性,但通过设置文本字段的委托并实现以下委托方法来获得此功能相对简单:

目标-C

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    // Prevent crashing undo bug – see note below.
    if(range.length + range.location > textField.text.length)
    {
        return NO;
    }

    NSUInteger newLength = [textField.text length] + [string length] - range.length;
    return newLength <= 25;
}

夫特

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

    let currentCharacterCount = textField.text?.characters.count ?? 0
    if (range.length + range.location > currentCharacterCount){
        return false
    }
    let newLength = currentCharacterCount + string.characters.count - range.length
    return newLength <= 25
}

修改

这是基于评论的更新的快速方法。

//  assume you have the following 3 fields
let textField1 = UITextField()
let textField2 = UITextField()
let textField3 = UITextField()

// then declare this dictionary
let lengthsDictionary = [textField1 : 10, textField2: 20, textField3: 30]


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

    guard let length = lengthsDictionary[textField] else {
        return true
    }

    let currentCharacterCount = textField.text?.characters.count ?? 0
    if (range.length + range.location > currentCharacterCount){
        return false
    }
    let newLength = currentCharacterCount + string.characters.count - range.length
    return newLength <= length
}

答案 2 :(得分:0)

如果要对多个文本字段的章程应用限制,请在IB中设置标记值。例如,您有2个文本字段需要文本限制为3,4所以为各个文本字段设置3和4标记,并在shouldChangeCharactersInRange方法中添加以下代码

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

    let characterCount = textField.text?.characters.count ?? 0
    if (range.length + range.location > characterCount){
        return false
    }
    let newLength = characterCount + string.characters.count - range.length
    return newLength <= textField.tag
}

答案 3 :(得分:0)

func textField(
    _ textField: UITextField,
    shouldChangeCharactersIn range: NSRange,
    replacementString string: String)
-> Bool {
    
    var currentText = ""
    if textField == passwordTextField{
        currentText = textField.text ?? ""
    } else if textField == againPasswordTextField{
        currentText = textField.text ?? ""
    }

    guard let stringRange = Range(range, in: currentText) else { return false }
    let updatedText = currentText.replacingCharacters(in: stringRange, with: string)
    return updatedText.count <= 6
}

}

为我工作:)