ios 设置`UITextView`和`UITextField`的最大字符数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32935528/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 07:54:20  来源:igfitidea点击:

Setting maximum number of characters of `UITextView ` and `UITextField `

iosswiftuitextfielduitextview

提问by biggreentree

I'd like to set a maximum number of characters allowed to be typed both in a UITextViewand a UITextField. This number will be then shown in a little label (for user's reference, Twitter Style.)

我想设置允许在 aUITextView和 a 中输入的最大字符数UITextField。然后这个数字将显示在一个小标签中(供用户参考,Twitter 样式。)

回答by Abhinav

Update Swift 4.X

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

Try this out:

试试这个:

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

回答by Maksim Kniazev

SWIFT 4

快速 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
}

回答by Bogdan Bystritskiy

Swift 4:

斯威夫特 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
}

回答by Abhijeet Mallick

Try this out:-

试试这个:-

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

回答by Junaid Mukhtar

Swift 3.0

斯威夫特 3.0

Just override this UITextFieldDelegate function, set the desired characterLimit variable and you are good to go:

只需覆盖此 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
}

回答by Arpit Jain

Swift 4/ Xcode 9.1

斯威夫特 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 
}

回答by Gerasim

Swift 5

斯威夫特 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
}

回答by Krunal Patel

Swift 4 , Xcode 9.1 GM

斯威夫特 4 , Xcode 9.1 通用

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