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.
-
Use https://github.com/hoteltonight/HTCopyableLabel – M.Y. May 12 '14 at 14:50
-
See my answer here https://stackoverflow.com/questions/1246198/show-iphone-cut-copy-paste-menu-on-uilabel/55805279#55805279 – Naresh Apr 23 '19 at 06:12
-
or ..https://stackoverflow.com/a/45541271/294884 – Fattie Jul 11 '19 at 18:42
3 Answers
It is not possible with UILabel
.
You should use UITextView
for that. Just disable editing using textFieldShouldBeginEditing
delegate method.

- 3,901
- 1
- 18
- 10

- 13,856
- 1
- 45
- 58
-
-
1I used UITextField few week ago and I remember that there were no border (it was created in xib). If your UITextField has a border, then just read documentation to find out how to disable border. – Yuras Nov 03 '10 at 20:06
-
3http://developer.apple.com/library/ios/#documentation/uikit/reference/UITextField_Class/Reference/UITextField.html [textField setBorderStyle:UITextBorderStyleNone] – Yuras Nov 03 '10 at 20:08
-
30You mean UITextView not UITextField, right? There is no `editable` property on UITextField. – Gregory Cosmo Haun Nov 13 '13 at 06:25
-
3Here is a category that does just that: https://github.com/alexandreos/UILabel-Copyable – Michael Peterson Apr 08 '16 at 23:38
-
3
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.

- 510,854
- 105
- 1,084
- 1,005
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:
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(textTapped:)];
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(textPressed:)];
[myLabel addGestureRecognizer:tap];
[myLabel addGestureRecognizer:longPress];
[myLabel setUserInteractionEnabled:YES];
Next up handle the gestures:
- (void) textPressed:(UILongPressGestureRecognizer *) gestureRecognizer {
if (gestureRecognizer.state == UIGestureRecognizerStateRecognized &&
[gestureRecognizer.view isKindOfClass:[UILabel class]]) {
UILabel *someLabel = (UILabel *)gestureRecognizer.view;
UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
[pasteboard setString:someLabel.text];
...
//let the user know you copied the text to the pasteboard and they can no paste it somewhere else
...
}
}
- (void) textTapped:(UITapGestureRecognizer *) gestureRecognizer {
if (gestureRecognizer.state == UIGestureRecognizerStateRecognized &&
[gestureRecognizer.view isKindOfClass:[UILabel class]]) {
UILabel *someLabel = (UILabel *)gestureRecognizer.view;
UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
[pasteboard setString:someLabel.text];
...
//let the user know you copied the text to the pasteboard and they can no paste it somewhere else
...
}
}

- 4,649
- 4
- 39
- 63

- 7,282
- 8
- 50
- 47
-
3nice answer, but you should add one line of code: myLabel.userInteractionEnabled = YES; – Ilario Mar 08 '16 at 09:45