0
我試圖調試一個問題,按下返回鍵不會觸發文本視圖更改,並因此在我的UITextView中使用我的NSAttributedString發生混淆。每當我按下返回按鈕時,我的UITextView會計算行數,並且出於任何原因認爲圖像只有1行。如何在UITextView中按下返回按鈕時調用自定義函數?
我試圖調試一個問題,按下返回鍵不會觸發文本視圖更改,並因此在我的UITextView中使用我的NSAttributedString發生混淆。每當我按下返回按鈕時,我的UITextView會計算行數,並且出於任何原因認爲圖像只有1行。如何在UITextView中按下返回按鈕時調用自定義函數?
您可以使用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
}
我正在使用自定義UITextView,但即使它是一個子類uitextview,此方法永遠不會被調用。我可以檢查textview如何改變,如輸入字符,但不是「\ n」 – ggworean
我測試過它,它適用於我。你有沒有設置'textView.delegate = self'? @ggworean – ale00
錯誤的擴展名,謝謝你現在正在工作! – ggworean