Use of the iOS copy sticker

Source: Internet
Author: User

In iOS, you can use the Clipboard to implement data sharing among applications and between applications. For example, you can copy a URL from iphone qq and paste it into the Safari browser to see the content of the link.

One, the following three controls in iOS, there is a copy-paste function itself:

1, Uitextview
2, Uitextfield
3, UIWebView

The UIKit framework provides several classes and protocols to facilitate our ability to implement the Clipboard in our own applications.

1, Uipasteboard: We can write data to it, can also read the data

2. Uimenucontroller: Displays a shortcut menu for copying, clipping, and pasting selected items.

3. Canperformaction:withsender in Uiresponder: Used to control which commands are displayed in the shortcut menu.

4. Uiresponderstandardeditactions will be called when the command on the shortcut menu is clicked.

Three, the following items can be placed on the Clipboard

1, uipasteboardtypeliststring-string array, including Kuttypeutf8plaintext
2, Uipasteboardtypelisturl-url array, including Kuttypeurl
3. uipasteboardtypelistimage-graphic array, including Kuttypepng and Kuttypejpeg
4, uipasteboardtypelistcolor-color array

Four, the type of Clipboard is divided into two kinds:

System level: The system-level clipboard is created with Uipasteboardnamegeneral and Uipasteboardnamefind, and the data is not lost when the application is closed or unloaded.

Application-level: By setting, you can have the data remain on the Clipboard after the application is closed, but the data is lost after the application is unloaded. We can use Pasteboardwithname:create: to create.

Use of Uimenucontroller, copy of Uilabel and custom menus

1. The view of the menu must be implemented – (BOOL) Canbecomefirstresponder, and return Yes

2. The view of the menu must be implemented – (BOOL) Canperformaction:withsender and return Yes or no as required

3. Make menu View a first Responder (Becomefirstresponder)

4. Position menu (-Settargetrect:inview:)

5. Display Menu (-setmenuvisible:animated:)

  1. @implementation Uicopylabel
  2. Default is NO
  3. -(BOOL) canbecomefirstresponder{
  4. return YES;
  5. }
  6. Features of "Feedback" care
  7. -(BOOL) Canperformaction: (SEL) Action Withsender: (ID) sender{
  8. return (Action = = @selector (copy:));
  9. }
  10. Implementation for copy
  11. -(void) copy: (ID) sender{
  12. Uipasteboard *pboard = [Uipasteboard Generalpasteboard];
  13. pboard.string = Self.text;
  14. }
  15. Uilabel The default is not to receive events, we need to add the touch event ourselves
  16. -(void) attachtaphandler{
  17. self.userinteractionenabled = YES; //master switch for user interaction
  18. UITapGestureRecognizer *touch = [[UITapGestureRecognizer alloc] initwithtarget:self action: @selector (Handletap:)];
  19. touch.numberoftapsrequired = 2;
  20. [Self addgesturerecognizer:touch];
  21. [Touch release];
  22. }
  23. Binding events
  24. -(ID) initWithFrame: (CGRect) frame
  25. {
  26. self = [super Initwithframe:frame];
  27. if (self) {
  28. [Self attachtaphandler];
  29. }
  30. return self;
  31. }
  32. Ditto
  33. -(void) awakefromnib{
  34. [Super awakefromnib];
  35. [Self attachtaphandler];
  36. }
  37. -(void) Handletap: (uigesturerecognizer*) recognizer{
  38. [Self becomefirstresponder];
  39. Uimenucontroller *menu = [Uimenucontroller Sharedmenucontroller];
  40. [Menu SetTargetRect:self.frame InView:self.superview];
  41. [Menu Setmenuvisible:yes Animated:yes];
  42. }
  43. @end

Add a Uicopylabel to the view

You can now use Uicopylabel to implement a double-click to copy the contents of the label

In your view

Uicopylabel *display = [[uicopylabelalloc]initwithframe: CGRectMake(100 , (+ )];

Awakefromnib

The use of this method is only involved when using IB, When the. nib file is loaded, a awakefromnib message is sent to each object in the. nib file, and each object can define its own awakefromnib function to respond to the message and perform some necessary actions.

See Example:

Create a Viewcontroller with XIB

Define a subclass of UIView

Open Xib and specify the type of view as the subclass defined in the previous step

Then add the awakefromnib method in TESTVIEW.M, run the program to find this method is called!!!

Now let's customize the menu.

add a long push gesture to the Attachtaphandler

  1. -(void) attachtaphandler{
  2. self.userinteractionenabled = YES; //master switch for user interaction
  3. //double-click
  4. UITapGestureRecognizer *touch = [[UITapGestureRecognizer alloc] initwithtarget:self action: @selector (Handletap:)];
  5. touch.numberoftapsrequired = 2;
  6. [Self addgesturerecognizer:touch];
  7. [Touch release];
  8. //Long Press
  9. Uilongpressgesturerecognizer *press = [[Uilongpressgesturerecognizer alloc]initwithtarget:self Action: @selector ( Longpress:)];
  10. Press.minimumpressduration = 1.0;
  11. [Self addgesturerecognizer:press];
  12. [Press release];
  13. }

Add Method longpress

  1. -(void) longpress: (Uilongpressgesturerecognizer *) recognizer {
  2. if (recognizer.state = = Uigesturerecognizerstatebegan) {
  3. Tstableviewcell *cell = (Tstableviewcell *) Recognizer.view;
  4. [Self becomefirstresponder];
  5. UIMenuItem *flag = [[UIMenuItem alloc] initwithtitle:@"flag" Action: @selector (flag:)];
  6. UIMenuItem *approve = [[UIMenuItem alloc] initwithtitle:@"approve" Action: @selector (approve:)];
  7. UIMenuItem *deny = [[UIMenuItem alloc] initwithtitle:@"Deny" Action: @selector (deny:)];
  8. Uimenucontroller *menu = [Uimenucontroller Sharedmenucontroller];
  9. [Menu Setmenuitems:[nsarray Arraywithobjects:flag, approve, deny, Nil]];
  10. [Menu SetTargetRect:self.frame InView:self.superview];
  11. [Menu Setmenuvisible:yes Animated:yes];
  12. NSLog (@"menuitems:%@", Menu.menuitems);
  13. }
  14. }
  15. -(void) flag: (ID) Sender {
  16. NSLog (@"Cell was flagged");
  17. }
  18. -(void) Approve: (ID) Sender {
  19. NSLog (@"Cell was approved");
  20. }
  21. -(void) deny: (ID) Sender {
  22. NSLog (@"Cell was denied");
  23. }

Modify Canperformaction

[CPP]View Plaincopyprint?
    1. -(BOOL) Canperformaction: (SEL) Action Withsender: (ID) sender{
    2. return (action = = @selector (copy:));
    3. if (action = = @selector (copy:) | | Action = = @selector (flag:) | | Action = = @selector (approve:) | | Action = = @selector (deny:)) {
    4. return YES;
    5. }
    6. }

Ok... Effect

Use of the iOS copy sticker

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.