61

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.

Jonathan.
  • 53,997
  • 54
  • 186
  • 290

3 Answers3

63

It is not possible with UILabel.

You should use UITextView for that. Just disable editing using textFieldShouldBeginEditing delegate method.

Chris Villa
  • 3,901
  • 1
  • 18
  • 10
Yuras
  • 13,856
  • 1
  • 45
  • 58
  • But that will have the 3D border doesn't it? – Jonathan. Nov 03 '10 at 20:01
  • 1
    I 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
  • 3
    http://developer.apple.com/library/ios/#documentation/uikit/reference/UITextField_Class/Reference/UITextField.html [textField setBorderStyle:UITextBorderStyleNone] – Yuras Nov 03 '10 at 20:08
  • 30
    You mean UITextView not UITextField, right? There is no `editable` property on UITextField. – Gregory Cosmo Haun Nov 13 '13 at 06:25
  • 3
    Here is a category that does just that: https://github.com/alexandreos/UILabel-Copyable – Michael Peterson Apr 08 '16 at 23:38
  • 3
    Actually, it IS possible with any `UIResponder` subclass. – kelin Apr 21 '17 at 08:08
35

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.

kennytm
  • 510,854
  • 105
  • 1,084
  • 1,005
26

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
            ...
    }
}
theDuncs
  • 4,649
  • 4
  • 39
  • 63
Michael Gaylord
  • 7,282
  • 8
  • 50
  • 47