Allow user to select text from UILabel to copy

IphoneObjective CUilabel

Iphone Problem Overview


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.

Iphone Solutions


Solution 1 - Iphone

It is not possible with UILabel.

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

Solution 2 - Iphone

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.

Solution 3 - Iphone

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
			...
	}
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJonathan.View Question on Stackoverflow
Solution 1 - IphoneYurasView Answer on Stackoverflow
Solution 2 - IphonekennytmView Answer on Stackoverflow
Solution 3 - IphoneMichael GaylordView Answer on Stackoverflow