web-dev-qa-db-ja.com

`UITextView`と `UITextField`の最大文字数を設定する

UITextViewUITextFieldの両方で入力できる文字の最大数を設定したいと思います。この番号は小さなラベルで表示されます(ユーザーの参照用、Twitterスタイル)。

29
biggreentree

更新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
}

これを試してください:

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

これを試してください:-

 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

Swift 3.

このUITextFieldDelegate関数をオーバーライドし、必要なcharacterLimit変数を設定するだけで準備完了です。

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

スイフト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