IOS study note 35 -- UIPasteboard/UIMenuController (1)

Source: Internet
Author: User

Let's talk about the results of today, the use of the clipboard and the custom clipboard.

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 released, huh, And the next version summarizes the use of the clipboard in UITableView. '
3 program diagrams are attached.

 


 


 

 

 

 

 

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.