9. Click the control to pop up the copy, paste, cut, and so on (UIMenuController). Copy, paste, and cut shortcut keys.

Source: Internet
Author: User

9. Click the control to pop up the copy, paste, cut, and so on (UIMenuController). Copy, paste, and cut shortcut keys.

By default, the following controls support UIMenuController

UITextField

UITextView

UIWedView

Taking UITable as an example, the copy, cut, and paste dialog box is displayed.

Overall Thinking:(Built-in text)

1. Create a UILabel class. If you want to use both storyboard and Xib in the future, you can call the awakeFromNib and initWithFrame methods and perform initialization at the same time.

2. Role of making UILabel the first responder:

Two methods are provided: canBecomeFirstResponder and can1_maction (two required values)

It also tells UIMenuController what operations are supported and how to handle these operations.

3. Create a UIMenuController object and set two conditions.

First, set the display range of the pop-up object.

Then, let the object be visible and animated.

4. Implementation of various operations on UIMenuController objects (copy, paste, cut, etc)

1 #import "ZWLabel.h"
 2 
 3 @implementation ZWLabel
 4
 5-(void) awakeFromNib
 6 {
 7 [self setup];
 8 }
 9-(instancetype) initWithFrame: (CGRect) frame
10 {
11 if (self = [super initWithFrame: frame]) {
12 [self setup];
13}
14 return self;
15}
16 / **
17 * Initial operation
18 * /
19-(void) setup
20 {
21 self.userInteractionEnabled = YES;
22 UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget: self action: @selector (labelClick)];
23 [self addGestureRecognizer: tap];
twenty four }
25 / **
26 * Click and operate
27 * /
28-(void) labelClick
29 {
30 // Make it the first responder, two methods will be provided after becomeFirstResponder and canPerformAction
31 // tell UIMenuController what operations are supported and how to handle them
32 [self becomeFirstResponder];
33
34 // Create menu controller
35 UIMenuController * menu = [UIMenuController sharedMenuController];
36 // In which area the popup menu is displayed (two ways)
37 // [menu setTargetRect: self.frame inView: self.superview];
38 [menu setTargetRect: self.bounds inView: self];
39 // Make menu visible
40 [menu setMenuVisible: YES animated: YES];
41}
42
43 / **
44 * Is label the first responder
45 * /
46-(BOOL) canBecomeFirstResponder
47 {
48 return YES;
49}
50
51 / **
52 * What operations can a label perform (such as copy, paste)
53 * /
54-(BOOL) canPerformAction: (SEL) action withSender: (id) sender
55 {
56 NSLog (@ "% @", NSStringFromSelector (action)); // You can print out which operations are supported here (such as copy, cut, paste, etc., see the bottom print result for details)
57 if (action == @selector (copy :) || action == @selector (cut :) || action == @selector (paste :)) return YES;
58 return NO;
59}
60
61-(void) copy: (UIMenuController *) menu
62 {
63 // Copy your own text to the pasteboard
64 UIPasteboard * board = [UIPasteboard generalPasteboard];
65 board.string = self.text;
66}
67-(void) cut: (UIMenuController *) menu
68 {
69 // Copy your own text to the pasteboard
70 [self copy: menu];
71 // Empty text
72 self.text = nil;
73}
74-(void) paste: (UIMenuController *) menu
75 {
76 UIPasteboard * board = [UIPasteboard generalPasteboard];
77 self.text = board.string;
78}

Use custom text

You need to move the entire data to viewController. Otherwise, even though all data is in UILabel and runs correctly, a bunch of warnings will be prompted.

That is, you do not need to create a new class that inherits from UILabel. You only need to add a UIMenuItem object to remove can1_maction and related methods. The Code is as follows:

Note: userInteractionEnabled needs to be checked in the label.

1 #import "ViewController.h"
 2 @interface ViewController ()
 3 @property (weak, nonatomic) IBOutlet UILabel * textLabel;
 4
 5 @end
 6
 7 @implementation ViewController
 8 
 9-(void) viewDidLoad {
10 [super viewDidLoad];
11 UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget: self action: @selector (labelClick)];
12 [self.textLabel addGestureRecognizer: tap];
13}
14
15 / **
16 * Click and operate
17 * /
18-(void) labelClick
19 {
20 // Make it the first responder, and two methods will be provided after becomeFirstResponder and canPerformAction
21 // At the same time tell UIMenuController which operations are supported and how these operations are handled
22 [self.textLabel becomeFirstResponder];
twenty three     
24 // Create menu controller
25 UIMenuController * menu = [UIMenuController sharedMenuController];
26
27 // in which area the menu is displayed
28 // [menu setTargetRect: self.frame inView: self.superview];
29 [menu setTargetRect: self.textLabel.bounds inView: self.textLabel];
30 // Make menu visible
31 [menu setMenuVisible: YES animated: YES];
32
33 // Custom menuItem
34 UIMenuItem * ding = [[UIMenuItem alloc] initWithTitle: @ "顶" action: @selector (ding :)];
35 UIMenuItem * replay = [[UIMenuItem alloc] initWithTitle: @ "Reply" action: @selector (replay :)];
36 UIMenuItem * report = [[UIMenuItem alloc] initWithTitle: @ "Report" action: @selector (report :)];
37 menu.menuItems = @ [ding, replay, report];
38}
39
40-(void) ding: (UIMenuController *) menu
41 {
42 NSLog (@ "% s% @", __ func __, menu);
43}
44-(void) replay: (UIMenuController *) menu
45 {
46 NSLog (@ "% s% @", __ func __, menu);
47}
48-(void) report: (UIMenuController *) menu
49 {
50 NSLog (@ "% s% @", __ func __, menu);
51}
52-(BOOL) canBecomeFirstResponder
53 {
54 return YES;
55}

Note:A strange problem occurs. When using a custom type, if you add a condition: When you click label again, the pop-up menu disappears and adds a judgment.

1 if (menu.isMenuVisible) {
2         [menu setMenuVisible:NO animated:YES];
3 } else {...}

You need to cut the-(void) canBecomeFirstResponder {} To ZWLabel. Otherwise, the second line will not be executed, and menu. isMenuVisible will always be in the stateless state !!

 

Supported methods are as follows:

 1 2016-07-13 22:34:35.099 UIMenuController[16825:1745455] cut:
 2 2016-07-13 22:34:35.100 UIMenuController[16825:1745455] copy:
 3 2016-07-13 22:34:35.100 UIMenuController[16825:1745455] select:
 4 2016-07-13 22:34:35.100 UIMenuController[16825:1745455] selectAll:
 5 2016-07-13 22:34:35.100 UIMenuController[16825:1745455] paste:
 6 2016-07-13 22:34:35.100 UIMenuController[16825:1745455] delete:
 7 2016-07-13 22:34:35.100 UIMenuController[16825:1745455] _promptForReplace:
 8 2016-07-13 22:34:35.100 UIMenuController[16825:1745455] _transliterateChinese:
 9 2016-07-13 22:34:35.101 UIMenuController[16825:1745455] _showTextStyleOptions:
10 2016-07-13 22:34:35.101 UIMenuController[16825:1745455] _define:
11 2016-07-13 22:34:35.101 UIMenuController[16825:1745455] _addShortcut:
12 2016-07-13 22:34:35.101 UIMenuController[16825:1745455] _accessibilitySpeak:
13 2016-07-13 22:34:35.101 UIMenuController[16825:1745455] _accessibilitySpeakLanguageSelection:
14 2016-07-13 22:34:35.101 UIMenuController[16825:1745455] _accessibilityPauseSpeaking:
15 2016-07-13 22:34:35.102 UIMenuController[16825:1745455] _share:
16 2016-07-13 22:34:35.102 UIMenuController[16825:1745455] makeTextWritingDirectionRightToLeft:
17 2016-07-13 22:34:35.102 UIMenuController[16825:1745455] makeTextWritingDirectionLeftToRight:


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.