web-dev-qa-db-fra.com

Définition du nombre maximum de caractères de `UITextView` et `UITextField`

Je voudrais définir un nombre maximal de caractères pouvant être saisis à la fois dans un UITextView et un UITextField. Ce numéro sera ensuite affiché dans une petite étiquette (pour référence de l'utilisateur, Twitter Style.)

29
biggreentree

Mise à jour Swift 4.X

func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
    let newText = (textView.text as NSString).replacingCharacters(in: range, with: text)
    let numberOfChars = newText.count
    return numberOfChars < 10    // 10 Limit Value
}

Essayez ceci:

func textView(textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool {
    let newText = (textView.text as NSString).stringByReplacingCharactersInRange(range, withString: text)
    let numberOfChars = newText.characters.count // for Swift use count(newText)
    return numberOfChars < 10;
}
82
Abhinav

Swift 4

func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
     let newText = (textView.text as NSString).replacingCharacters(in: range, with: text)
     return newText.count < 10
}
35
Maksim Kniazev

Swift 4:

func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
  let newText = (textView.text as NSString).replacingCharacters(in: range, with: text)
  return newText.count <= 70
}
12
Bogdan Bystritskiy

Essayez ceci: -

 func textView(textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool {
    print("chars \(textView.text.characters.count) \( text)")

    if(textView.text.characters.count > 20 && range.length == 0) {
        print("Please summarize in 20 characters or less")
        return false;
    }

    return true;
}
9
Abhijeet Mallick

Swift 3.

Remplacez simplement cette fonction UITextFieldDelegate, définissez la variable characterLimit souhaitée et vous êtes prêt à partir:

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool 
{        
        let characterLimit = 5
        let newText = NSString(string: textField.text!).replacingCharacters(in: range, with: string)
        let numberOfChars = newText.characters.count
        return numberOfChars < characterLimit
}
5
Junaid Mukhtar

Swift 4/Xcode 9.1

func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {

    let currentText = textView.text ?? ""
    guard let stringRange = Range(range, in: currentText) else { return false }
    let changedText = currentText.replacingCharacters(in: stringRange, with: text)
    return changedText.count <= 399 // Pass your character count here 
}
3
Arpit Jain

Swift 4, Xcode 9.1 GM

func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
    adjustFrames()
    return textView.text.count + (text.count - range.length) <= 200
}
1
Krunal Patel

Swift 5

func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
    var newText = textView.text!
    newText.removeAll { (character) -> Bool in
        return character == " " || character == "\n"
    }

    return (newText.count + text.count) <= 40
}
0
Gerasim