Example code of long press copy function in uilabel of IOS

Time:2021-4-9

preface

There are many ways to add long press copy function to label on the Internet. To realize long press copy on uilabel, I use uimenucontroller. In uitextview and uitextfield, you have brought this thing, but you need to customize it in uilabel.

In view of the fact that some friends have little contact with uimenucontroller, here are some basic knowledge.

The uimenucontroller can use the system’s own methods or customize them.

By default, the system supports the uimenucontroller related operations of uitextfield, uitextview and uiwebview controls

For more use of uimenucontroller, please refer to this article:// www.jb51.net/article/87000.htm

Common system method and use


- (void)cut:(nullable id)sender NS_AVAILABLE_IOS(3_0);
- (void)copy:(nullable id)sender NS_AVAILABLE_IOS(3_0);
- (void)paste:(nullable id)sender NS_AVAILABLE_IOS(3_0);
- (void)select:(nullable id)sender NS_AVAILABLE_IOS(3_0);
- (void)selectAll:(nullable id)sender NS_AVAILABLE_IOS(3_0);
- (void)delete:(nullable id)sender NS_AVAILABLE_IOS(3_2);

It can be seen from the literal meaning that they are cut, copy, paste, select, select all and delete. It’s easy to use.

//For example, in a uitextview, I want to add the methods of select all and copy
//Just add this line of code when customizing uitextview
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
 if(action == @selector(selectAll:) || action == @selector(copy:)) return YES;
 return NO;
}

Careful friends may find that the last long pressed words are all in English. How can we change them into Chinese? As shown in the figure, add Chinese (Simplified) in project, info and localizations.

Custom method and use

Back to the topic, we want to add long press copy event to uilabel, but it does not support uimenucontroller, so let’s talk about custom methods.

Customize a uilabel and set the label to be the first responder


- (BOOL)canBecomeFirstResponder {
 return YES;
}

Set the long press event to call this method during initialization

- (void)setUp {
 /*You can add some code here, such as font, center, night mode and so on*/
 self.userInteractionEnabled = YES;
 [self addGestureRecognizer:[[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPress)]];
}

Long press the event to create a new uimenucontroller

- (void)longPress {
 
 //Set label as the first responder
 [self becomeFirstResponder];
 
 //Custom uimenucontroller
 UIMenuController * menu = [UIMenuController sharedMenuController];
 Uimenuitem * Item1 = [[uimenuitem alloc] initwithtitle: @ "copy" action:@selector (copyText:)];
 menu.menuItems = @[item1];
 [menu setTargetRect:self.bounds inView:self];
 [menu setMenuVisible:YES animated:YES];
}

Set label to perform those tasks

- (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
 if(action == @selector(copyText:)) return YES;
 return NO;
}

//If you copy the above and write the following code, the program will crash after clicking
if(action == @selector(selectAll:) || action == @selector(copy:)) return YES;

The concrete implementation of the method

- (void)copyText:(UIMenuController *)menu {
 //End method without text
 if (!self.text) return;
 //Copy text to clipboard
 UIPasteboard * paste = [UIPasteboard generalPasteboard];
 paste.string = self.text;
 
}

Final effect:

Attached with demo (local download), the user-defined uilabel can be directly dragged away for use

summary

The above is the whole content of this article, I hope the content of this article has a certain reference value for your study or work, if you have any questions, you can leave a message to exchange, thank you for your support for developer.