This question already has an answer here:
I have a UILabel, but how can I allow the user to select a portion of it's text. I don't want the user to be able to edit the text nor the label/textfield to have a border.
This question already has an answer here:
I have a UILabel, but how can I allow the user to select a portion of it's text. I don't want the user to be able to edit the text nor the label/textfield to have a border.
A poor man's version of copy and paste, if you cannot, or don't need to use a text view, would be to add a gesture recognizer to the label and then just copy the entire text to the pasteboard. It's not possible to do just a portion unless you use a
UITextView
Make sure you let the user know it's been copied and that you support both a single tap gesture as well as a long press, as it will pick up users trying to highlight a portion of text. Here is a bit of sample code to get you started:
Register the gesture recognizers on your label when you create it:
Next up handle the gestures:
You use create a UITextView and make its
.editable
to NO. Then you have a text view which (1) the user cannot edit (2) have no border and (3) the user can select text from it.It is not possible with
UILabel
.You should use
UITextField
for that. Just disable editing usingtextFieldShouldBeginEditing
delegate method.