簡體   English   中英

在UITextView中按下返回按鈕時,如何調用自定義函數?

[英]How can I call a custom function when the return button is pressed within a UITextView?

我正在嘗試調試一個問題,即按回車鍵不會觸發文本視圖更改,結果使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
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM