小编典典

设置UITextView和UITextField的最大字符数

swift

我想设置允许在a
UITextView和a中键入的最大字符数UITextField。然后,该数字将显示在一个小标签中(供用户参考,Twitter风格。)


阅读 354

收藏
2020-07-07

共1个答案

小编典典

更新 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;
}
2020-07-07