ios - 通过在 textField Swift 中点击文本来识别文本

标签 ios swift uitextview

我是新手,想为 UITextView 创建一个自定义类,我可以通过在 UITextView 中点击它来获取文本。如何创建自定义 UITextView 类?

最佳答案

试试这个,我已经创建了自定义的 textview 类和协议(protocol),你需要在你的类中确认并实现该方法以在每次单击 textview 时获取 textview 的文本。

class CustomTextView: UITextView {

    weak var delegateCustomTV: CustomTextViewDelegate?

    override func awakeFromNib() {
        super.awakeFromNib()
        let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(self.textViewTapped))
        self.addGestureRecognizer(tapGestureRecognizer)
    }

    @objc func textViewTapped() {
        delegateCustomTV?.preparedText(text: self.text ?? "")
    }
}

protocol CustomTextViewDelegate: class {
   func preparedText(text: String)
}

像我在下面使用的那样使用,

class yourViewController: UIViewController, CustomTextViewDelegate {


    @IBOutlet weak var textView: CustomTextView!

    override func viewDidLoad() {
        super.viewDidLoad()
        textView.delegateCustomTV = self
    }

    func preparedText(text: String) {
        // You will get your text here when you click on textview
        print(text)
    }
}

关于ios - 通过在 textField Swift 中点击文本来识别文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56124155/

相关文章:

iphone - 将相机模式设为黑白

swift - 程序崩溃并在调试器中显示以下内容和 "lldb"- Swift 4

ios - NS错误代码检查: Binary operator '==' cannot be applied to two Int operands

ios - 如何在 iPad 上使 UITextView 大小相同(Swift)

ios - 是否有任何通用的解决方案可以在屏幕上的键盘上方滚动 UITextView 和 UITextField

iphone - 在 UITextView 中垂直居中文本

ios - 调整 UITextView 的大小 - 高度不调整

ios - 应用程序崩溃(段错误 : 11) when turning on voice over

ios - 如何在用户输入后一致地将 UITextField 光标居中

ios - Xamarin iOS : Edit cells of UICollectionView