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.