Uimenucontroller and Uilabel

Source: Internet
Author: User

Recent projects, there is a project to use Pickerview for date selection, that is, click TextField, the bottom of the popup is not the keyboard, but pickerview. As required, this textfield is not editable, because the logic of pop-up Pickerview to replace the keyboard has been written, I only need to do two things:

(1) Remove the fork on the right side of this TextField (used to click Delete entire line)

Textfield.clearbuttonmode=uitextfieldviewnever;

(2) Disable long press TextField appear uimenucontroller (select/Select All/cut/paste, etc.)

The first thing is very easy, in order to make the second thing, I specially studied the use of Uimenucontroller. This post mainly records how I associate Uimenucontroller with Uilabel (Uilabel does not have this feature by default).


First, to be clear, the long press Uilabel appears uimenucontroller, this function is based on Uilabel, that is, which control to add this feature on who to base. The basis for this is to define the class to which the function is involved. Since Uilabel is a system class, it is used in the category way.


Second, define the function

There are 2 key functions involved: (override is overridden for a function)

-(BOOL) canbecomefirstresponder;//(override) Be sure to return yes-(BOOL) Canperformaction: (SEL) Action Withsender: (ID) sender;// (override) allows system UIMenuItem (select/Select All/cut/paste, etc.) to take effect and will be called multiple times

In addition, since Uilabel itself does not carry gesture, it is defined as:

-(void) attachlongpressgesture;//add gesture-(void) handlelongpress;//function to handle gestures

Finally, add the Uimenucontroller on the user click copy/paste handler function

-(void) copy: (ID) sender;//(override)-(void) paste: (ID) sender;//(override)


Iii. specific functions (add Files Uilabel+extension.h and UILABEL+EXTENSION.M)

Uilabel+extension.h:

#import <UIKit/UIKit.h> @interface UILabel (Extension)//Add a gesture to a label, defined here to make it easy for a label to call directly-(void) Attachlongpressgesture; @end


UILABEL+EXTENSION.M:

#import   "Uilabel+extension.h" @implementation  UILabel  (Extension)/* must allow UILabel to be a firstresponder */-  (BOOL) Canbecomefirstresponder{    return yes;} /* Customize which actions take effect */-  (BOOL) canperformaction: (SEL) Action withsender: (ID) sender{    if ( action ==  @selector (copy:)  | |  action [email protected] (paste:)) {        return  Yes;    }    return no;} /*label is directly used to add gestures */-  (void) Attachlongpressgesture{    self.userinteractionenabled=yes ;//allow interaction with the user         uilongpressgesturerecognizer *tap=[[ Uilongpressgesturerecognizer alloc] initwithtarget:selfaction: @selector (handlelongpress)];     [self addgesturerecognizer:tap];//Add gesture}/* gesture handler function */-  (void) handlelongpress{     if (![ Self becomefirstresponder]) {        return;    }     uimenucontroller *menu=[uimenucontroller sharedmenucontroller];    [menu  Settargetrect:self.frame inview:self.superview];    [menu setmenuvisible:yes  animated:yes];} /* Rewrite the copy function */-  (void) copy: (ID) sender{    uipasteboard *pasteboard=[uipasteboard  generalpasteboard];    pasteboard.string=self.text;} /* Override paste Function */-  (void) paste: (ID) sender{    uipasteboard *pasteboard=[uipasteboard  generalpasteboard];    if (pasteboard.string !=nil && ![ pasteboard.string isequaltostring:@ ""]    ) {         [email protected] "";         self.text= pasteboard.string;    }} @end

Iv. Use of

UILabel *testlabel=[[uilabel alloc] Initwithframe:cgrectmake (20,30, 100, 30)]; [Email protected] "Layne Zhang"; [TestLabel attachlongpressgesture];//Add gesture

In this way, long press TestLabel will appear Uimenucontroller, and only copy and Paste,copy and paste also have the corresponding function.

This article is from the "Layne Learning Corner" blog, please be sure to keep this source http://laynestone.blog.51cto.com/9459455/1680363

Uimenucontroller and 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.