如何使UITextView在Swift中可选

时间:2023-01-22 22:54:16

I have this UITextView in Swift:

我有这个UITextView在Swift:

let contactText = UITextView(frame: CGRectMake(0,0,200,50))
contactText.selectable = true
contactText.dataDetectorTypes = UIDataDetectorTypes.Link
contactText.userInteractionEnabled = true
contactText.editable = false 
contactText.text = "Some text goes here and some website here www.google.com"
self.view.addSubview(contactText)

I want to be able to double tap or tap and hold to select the text and then give the user the option to select all or copy (just like most apps do). The above code didn't work although it seems like it should, is there another way of doing it?

我希望能够双击或按住来选择文本,然后给用户选择全部或复制的选项(就像大多数应用程序那样)。上面的代码不起作用,虽然看起来应该这样,但是还有其他方法吗?

EDIT:

编辑:

The above code actually works, the reason why is not working (not 100% sure yet) is probably because the UITextView is within a UIView that has a PanGestureRecognizer attached to it, so maybe that is blocking it? any ideas?

上面的代码实际上是有效的,为什么不能工作(不是100%确定)可能是因为UITextView在一个UIView中它有一个pangesturerecnizer附加在它上面,所以这可能会阻止它?什么好主意吗?

1 个解决方案

#1


1  

To make a UITextView selectable you only need to add these two lines:

要使UITextView具有可选性,只需添加以下两行:

yourTextView.selectable = true

yourTextView。选择= true

yourTextView.userInteractionEnabled = true

yourTextView。userInteractionEnabled = true

#1


1  

To make a UITextView selectable you only need to add these two lines:

要使UITextView具有可选性,只需添加以下两行:

yourTextView.selectable = true

yourTextView。选择= true

yourTextView.userInteractionEnabled = true

yourTextView。userInteractionEnabled = true