[Go]ios add long press Copy to Uilabel

Source: Internet
Author: User

The following three controls in iOS have the ability to copy-paste themselves:

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 to display items for selection, such as copy, clip, paste, and so on.
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.

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

The type of clipboard is divided into two types:
System level: Using Uipasteboardnamegeneral and Uipasteboardnamefind, system-level applications shut down, or unloaded data is not lost.

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.

Examples are as follows:

Sometimes we may need to copy the text on the Uilabel, or the Uiimageview picture, and Uilabel and Uiimageview do not respond to the touch event or copy, then we need to implement a replicable uilabel ourselves. Add a new class to inherit from Uilabel:

@interface Uicopylabel:uilabel  @end  #import "UICopyLabel.h"  @implementation uicopylabel  @end  

In order to receive the event (which can be the first responder), we need to override a method:

-(BOOL) canbecomefirstresponder{      return YES;    

There are also two methods that need to be covered for replication operations:

The method that can be responded to (BOOL) Canperformaction: (SEL) Action Withsender: (ID) sender{      return (action = = @selector (copy:));  }  

Implementation for Response method  -(void) copy: (ID) sender{      uipasteboard *pboard = [Uipasteboard Generalpasteboard];      pboard.string = Self.text;  }  

With the above three methods, we can handle copy, of course, in case of receiving the event:

Uilabel The default is not to receive events, we need to add the touch event ourselves  -(void) attachtaphandler{      self.userinteractionenabled = YES;  Total switch for user interaction      UITapGestureRecognizer *touch = [[UITapGestureRecognizer alloc] initwithtarget:self action: @selector ( Handletap:)];      touch.numberoftapsrequired = 2;      [Self addgesturerecognizer:touch];      [Touch release];  }  Bind event  -(ID) initWithFrame: (CGRect) frame  {Self      = [Super Initwithframe:frame];      if (self)   {          [self attachtaphandler];      }      return self;  }  Ibid  . (void) awakefromnib{      [Super awakefromnib];      [Self Attachtaphandler];  }  

We are ready to receive the event! Since I set the tap number to 2 above, so I need to double-click to capture, next, we need to deal with this tap, so that the menu bar pop out:

-(void) Handletap: (uigesturerecognizer*) recognizer{   [self becomefirstresponder]; UIMenuItem *copylink = [[[Uimenuitemalloc] initwithtitle:@ "copy"                                                          action: @selector (copy:)]autorelease]; [[Uimenucontrollersharedmenucontroller] setmenuitems:[nsarrayarraywithobjects:copylink, nil]]; [[Uimenucontrollersharedmenucontroller] setTargetRect:self.frameinView:self.superview]; [[Uimenucontrollersharedmenucontroller] setMenuVisible:YESanimated:YES];}  

In this way, a replicable Uilabel is born! It can handle receive clicks, pop-up menu bars, handle copy, which is a very common replicable control.

Next we make a replicable uiimageview, create a new Viewcontroller, put two imageview, and show different graphs by default:
Then copy the above code directly over to three places:

-(BOOL) Canperformaction: (SEL) Action Withsender: (ID) sender{      return (action = = @selector (copy:) | | action = = Selector (paste:));  }    -(void) copy: (ID) sender{      uipasteboard *pboard = [Uipasteboard Generalpasteboard];      Pboard.image = Self.image;  }    -(void) paste: (ID) sender{      uipasteboard *pboard = [Uipasteboard Generalpasteboard];      Self.image = Pboard.image;  }  

Uipasteboard has both system-level and application-level types, so not only can it communicate within the application, but it can also communicate between applications, such as I copy a URL, then open Safari, paste it into the address bar, and we can communicate and share data between applications.

In the Pasteboardwrite, click "Write" after the text in the TextField to the pasteboard, and then switch to Pasteboardread when the display. If we only want to give "one of our own", we can not use the system of the universal pasteboard, we need to create a:

Need to provide a unique name, generally using the inverted domain name: Com.mycompany.myapp.pboard  //The following parameter indicates whether to create a uipasteboard *PB = if it does not exist  = [ Uipasteboard pasteboardwithname:@ "Testboard" create:yes];  

Using this clipboard, we can put the text in, and then read it in another app, some of the common types have been set to properties:


In addition, if the data types that are capable of converting to plist (NSString, Nsarray, Nsdictionary, NSDate, NSNumber, and Nsurl), we can call Setvalue:forpasteboardtype: method to store data, other types can only call Setdata:forpasteboardtype: Method (plist data type can also be used), similar to this:

Store data nsdictionary *dict = [nsdictionary dictionaryWithObject:textField.text forkey:@ "content"];  NSData *dictdata = [Nskeyedarchiver archiveddatawithrootobject:dict];  [PB setdata:dictdata forpasteboardtype:@ "MyType"];  Get is similar to this: Uipasteboard *PB = [Uipasteboard pasteboardwithname:@ "Testboard" create:yes];  Nsdictionary *dict = [nskeyedunarchiver unarchiveobjectwithdata:[pb dataforpasteboardtype:@ "MyType"]];  Caption.text = [dict objectforkey:@ "content"];  

A pasteboardtype is mentioned above, which is a uniform type identifier (Uniform type Identifier UTI) that helps the app to get the data it can handle. For example, you can only handle the text paste, that gives you a uiimage is obviously useless. You can use a common UTI, or you can use any character, and Apple recommends using the inverted domain name plus the type name: Com.myCompany.myApp.myType.

[Go]ios add long press Copy to Uilabel

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.