Limit length of multiple UITextFields in Swift 2

时间秒杀一切 提交于 2019-12-08 07:17:10

问题


a solution to limit the length of a TextField but the function count has been updated, also count() function, so I don't understand how to use this:

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
}

And how to update this to work on multiple TextField , I think I have to say if

textField = thisTextField.text...

回答1:


shouldChangeCharactersInRange does the following (Quoted from the docs)

Asks the delegate if the specified text should be changed.

Your added code to this method checks if it exceeds your limit (In your example, it is 10) and returns false which means that the textField should not change values. If it did not exceed the limit, it will return true, and the textField will change values.

To do this for multiple textFields, you will need to have outlets to your multiple textFields, and then a simple if statement inside this method will do the job.

@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
}

To be able to use the above method in your code, your UIViewController which contains these textFields will need to implement the UITextFieldDelegate protocol, and then by setting the UITextField's delegate property to be that UIViewController.

Also regarding the count method. It has been updated many times. To count the number of characters for a string:

Before Swift1.2 -> countElements(string)
Swift1.2 -> count(string)
Swift2 -> string.characters.count




回答2:


From this answer: Set the maximum character length of a UITextField

While the UITextField class has no max length property, it's relatively simple to get this functionality by setting the text field's delegate and implementing the following delegate method:

Objective-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;
}

Swift

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
}

EDIT

Here is an updated swift method based on the comment.

//  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
}



回答3:


If you want to apply limit on charters for multiple textfields set tag value in IB. For example you have 2 text fields required text limit is 3,4 so set 3 and 4 tag for respective text fields and add below code in shouldChangeCharactersInRange method

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
}


来源:https://stackoverflow.com/questions/34412115/limit-length-of-multiple-uitextfields-in-swift-2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!