IOS clipboard version UIPasteboard/UIMenuController

Source: Internet
Author: User

IOS clipboard version UIPasteboard/UIMenuController

Direct call of system clipboard

In fact, the whole process is very simple. I will use a custom UILable I wrote to call the system clipboard.

First, because Apple only releases the clipboard of the UITextView, UITextField, and webView controls, the clipboard of the general controls is disabled. Therefore, the first thing we need to do is to put this attribute out. In fact, there are three simple methods:

Response Method

-(BOOL) canBecomeFirstResponder {

ReturnYES;

}

// "Feedback" refers to the function of interest, that is, to release the function you need. For example, if you want to release copy, you will return YES; otherwise, NO;

-(BOOL) can1_maction :( SEL) action withSender :( id) sender {

If (action ==@ selector (copy :)){

ReturnYES;

}

Else if (action ==@ selector (paste :)){

Return YES;

}

Else if (action ==@ selector (cut :)){

ReturnNO;

}

Else if (action ==@ selector (select :)){

ReturnNO;

}

Else if (action ==@ selector (delete :)){

ReturnNO;

}

Return NO;

}

OK. The clipboard function of the open control has been released, and the rest is implemented.


// UILabel does not receive events by default. We need to add touch events by ourselves.

-(Void) attachTapHandler {

Self. userInteractionEnabled = YES; // user interaction Switch

UITapGestureRecognizer * touch = [[UITapGestureRecognizeralloc] initWithTarget: selfaction: @ selector (handleTap :)];

Touch. numberOfTapsRequired = 1;

[SelfaddGestureRecognizer: touch];

[Touch release];

}

Response to click events

-(Void) handleTap :( UIGestureRecognizer *) recognizer {

[SelfbecomeFirstResponder];

UIMenuController * menu = [UIMenuControllersharedMenuController];

[Menu setTargetRect: self. frameinView: self. superview];

[Menu setMenuVisible: YESanimated: YES];

}

OK. Now you can click the COPY menu. The following is the implementation of copy and paste.


// For copy implementation

-(Void) copy :( id) sender {

 

UIPasteboard * pboard = [UIPasteboardgeneralPasteboard];

Pboard. string = self. text;

}

 


-(Void) paste :( id) sender {

 

Self. textAlignment = UITextAlignmentRight;

UIPasteboard * pboard = [UIPasteboardgeneralPasteboard];

Self. text = [NSStringstringWithFormat: @ "paste content: % @", pboard. string];

NSLog (@ "pboard. string: % @", pboard. string );

}

Since then, a replicable UILabel has been moved to the empty world. Haha, in summary, we will summarize the use of clipboard in UITableView. '
3 program diagrams are attached.

 

 

Two ways to use clipboard on UITableView;

I. Use in custom Cells

In fact, cell usage is basically the same as label usage.

1. Release Method:


-(BOOL) canBecomeFirstResponder {

Return YES;

}

-(BOOL) can1_maction :( SEL) action withSender :( id) sender {

If (action ==@ selector (cut :)){

Return NO;

}

Else if (action ==@ selector (copy :)){

Return YES;

}

Else if (action ==@ selector (paste :)){

Return NO;

}

Else if (action ==@ selector (select :)){

Return NO;

}

Else if (action ==@ selector (selectAll :)){

Return NO;

}

Else {

Return [super canPerformAction: action withSender: sender];

}

}

Then, of course, the copy method is implemented ..

-(Void) copy :( id) sender {

NSLog (@ "copy ");

UIPasteboard * pasteboard = [UIPasteboard generalPasteboard];

[Pasteboard setString: [[self textLabel] text];

}

However, the most important step is to use this method to stimulate the copy menu.


-(Void) setHighlighted :( BOOL) highlighted animated :( BOOL) animated {

[[Self delegate] performSelector: @ selector (showMenu :)

WithObject: self afterDelay: 0.9f];

[Super setHighlighted: highlighted animated: animated];

}

As for the Menu Generation, you are familiar with the following code:


// Display menu

-(Void) showMenu :( id) cell {

If ([cell isHighlighted]) {

[Cell becomeFirstResponder];

UIMenuController * menu = [UIMenuController sharedMenuController];

[Menu setTargetRect: [cell frame] inView: [self view];

[Menu setMenuVisible: YES animated: YES];

}

}

Okay, the rest is about how to use a custom cell. You should be familiar with it. I will not introduce it one by one. Since then, the first method of using clipboard has been introduced.
II. The second method is simpler, because (_ MAC_NA ,__ IPHONE_5_0) three methods have been released, which is very easy to use. I will directly use Farah above.


-(BOOL) tableView :( UITableView *) tableView shouldShowMenuForRowAtIndexPath :( NSIndexPath *) indexPath {

 

Return YES;

}

 


-(BOOL) tableView :( UITableView *) tableView can1_maction :( SEL) action forRowAtIndexPath :( NSIndexPath *) indexPath withSender :( id) sender {

If (action ==@ selector (copy :)){

Return YES;

}

 

Return NO;

}

 


-(Void) tableView :( UITableView *) tableView upload maction :( SEL) action forRowAtIndexPath :( NSIndexPath *) indexPath withSender :( id) sender {

 

UITableViewCell * cell = [tableView cellForRowAtIndexPath: indexPath];

If (action ==@ selector (copy :)){

[UIPasteboard generalPasteboard]. string = cell. textLabel. text;

}

 

}

With the above three methods, I think you can easily use the clipboard in the cell ..

 

Summary: Use of the custom clipboard

In fact, the custom clipboard is also very simple, it is nothing more than releasing the response time, using UIMenuController to customize the clipboard, and then the most critical implementation of the copy method you use to pull.

For convenience and practicality, I added a long-press event to the cell and read the code ---

 

UILongPressGestureRecognizer * recognizer = [[UILongPressGestureRecognizer alloc] initWithTarget: self action: @ selector (longPress :)];

[Cell addGestureRecognizer: recognizer];

Okay, the event is added, and the remaining click is to implement the custom clipboard. The Code is as follows:

 

-(Void) longPress :( UILongPressGestureRecognizer *) recognizer {

If (recognizer. state = UIGestureRecognizerStateBegan ){

CopyCell * cell = (CopyCell *) recognizer. view;

[Cell becomeFirstResponder];

UIMenuItem * flag = [[UIMenuItem alloc] initWithTitle: @ "Flag" action: @ selector (flag :)];

UIMenuItem * approve = [[UIMenuItem alloc] initWithTitle: @ "Approve" action: @ selector (approve :)];

UIMenuItem * deny = [[UIMenuItem alloc] initWithTitle: @ "Deny" action: @ selector (deny :)];

UIMenuController * menu = [UIMenuController sharedMenuController];

 

[Menu setMenuItems: [NSArray arrayWithObjects: flag, approve, deny, nil];

 

NSLog (@ "... % @", NSStringFromCGRect (cell. frame ));

[Menu setTargetRect: cell. frame inView: cell. superview];

 

[Menu setMenuVisible: YES animated: YES];

}

}

Through the above Code, the custom clipboard is successfully completed, run, you will suddenly find that, fuck, how did the Clipboard not come out, I guess you already think of why, because

-(BOOL) canBecomeFirstResponder {

Return YES;

}

There is no such important sentence. OK. The following is the method you want to use.

 

-(Void) flag :( id) sender {

 

NSLog (@ "Cell was flagged ");

 

}

-(Void) approve :( id) sender {

 

NSLog (@ "Cell was approved ");

}

 

-(Void) deny :( id) sender {

 

NSLog (@ "Cell was denied ");

 

}

Well, this is the basic clipboard customization. To make it more intuitive, the above three articles are similar to one of the following:


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.