我试图将 TextView 中的字符数限制为 20 个。20 个之后它应该改为“...”。该函数未触发,我正在正确设置委托(delegate)。
动物类
cell.pn.text = np[indexPath.row]
cell.pn.selectable = false
cell.pn.delegate = self
Animal类的扩展
extension Animal : UITextViewDelegate{
func textView(textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool {
return textView.text.characters.count + (text.characters.count - range.length) <= 20
}
}
最佳答案
试试这个:
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
let text = textField.text
let newLength = text.characters.count + string.characters.count - range.length
return newLength <= 20
}
关于ios - 如何在 20 个字符后切断 TextView ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37665215/