我正在尝试调试一个问题,即按回车键不会触发文本视图更改,结果使UITextView内部的NSAttributedString混乱。每当我按下返回按钮时,我的UITextView都会计算行数,并且无论出于何种原因,都认为图像只有1行。
您可以使用调用自定义函数textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String)
。
class ViewController: UIViewController, UITextViewDelegate {
@IBOutlet weak var textView: UITextView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
textView.delegate = self
}
func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
if text == "\n" {
print("new line")
// Call your custom function
}
return true
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句