gpt4 book ai didi

ios - 在 UITextView 中按下返回按钮时如何调用自定义函数?

转载 作者:行者123 更新时间:2023-11-28 16:16:14 28 4
gpt4 key购买 nike

我正在尝试调试一个问题,即按回车键不会触发 TextView 更改,结果会弄乱我的 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
}

关于ios - 在 UITextView 中按下返回按钮时如何调用自定义函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39051947/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com