A uilabel that triggers a click event and changes color

Source: Internet
Author: User

A uilabel that triggers a click event and changes color

Who said that uilabel could not process click events as a button? Today, I want to provide a modified uilabel that can trigger click events and change color :)

:

You can also use timer as a Timer:

The source code is as follows:

Taplabel. h and taplabel. m

//// Taplabel. h // taplabel // copyright (c) 2014 y. x. all rights reserved. // # import <uikit/uikit. h> @ Class taplabel; @ protocol taplabeldelegate <nsobject>-(void) taplabelevent :( taplabel *) label; @ end @ interface taplabel: uilabel @ property (nonatomic, assign) ID <taplabeldelegate> delegate; // Protocol @ property (nonatomic, strong) nsstring * icationicationname; // set the name of the notification center @ property (nonatomic, strong) nsdictionary * metadata; // metadata @ end
//// Taplabel. M // taplabel // copyright (c) 2014 y. x. all rights reserved. // # import "taplabel. H "@ implementation taplabel-(ID) initwithframe :( cgrect) frame {self = [Super initwithframe: frame]; If (Self) {self. userinteractionenabled = yes; uilongpressgesturerecognizer * tap = [[uilongpressgesturerecognizer alloc] initwithtarget: Self action: @ selector (labelevent :)]; tap. minimumpressduration = 0.01f; [self addgesturerecognizer: tap];} return self;}-(void) labelevent :( uilongpressgesturerecognizer *) gesture {// obtain the coordinate value cgpoint locationpoint = [gesture locationinview: self]; // status 1 If (gesture. state = uigesturerecognizerstatebegan) {self. highlighted = yes;} // status 2 if (gesture. state = uigesturerecognizerstatechanged) {If (locationpoint. x <= self. bounds. size. width & locationpoint. x> = 0 & locationpoint. Y <= self. bounds. size. height & locationpoint. y> = 0) {self. highlighted = yes;} else {self. highlighted = no; }}// status 3 if (gesture. state = uigesturerecognizerstateended) {If (locationpoint. x <= self. bounds. size. width & locationpoint. x> = 0 & locationpoint. Y <= self. bounds. size. height & locationpoint. y> = 0) {If (_ delegate) {[_ delegate taplabelevent: Self];} If (_ icationicationname) {[[nsicationicationcenter defacenter center] postnotificationname: _ icationicationname object: nil userinfo: @ {@ "taplabel": Self}] ;}} self. highlighted = no; }}@ end

Source code for use:

//// Rootviewcontroller. M // taplabel // copyright (c) 2014 y. x. all rights reserved. // # import "rootviewcontroller. H "# import" taplabel. H "@ interface rootviewcontroller () <taplabeldelegate> @ end @ implementation rootviewcontroller-(void) viewdidload {[Super viewdidload]; taplabel * tap = [[taplabel alloc] initwithframe: cgrectmake (0, 0,320, 20)]; tap. textalignment = nstextalignmentcenter; tap. center = self. view. center; tap. TEXT = @ "youxianming"; tap. font = [uifont fontwithname: @ "helveticaneue-thin" Size: 18]; tap. delegate = self; tap. metadata =@{@ "name": @ "youxianming"}; tap. highlightedtextcolor = [uicolor redcolor]; [self. view addsubview: tap];}-(void) taplabelevent :( taplabel *) label {nslog (@ "% @", label. metadata);} @ end

Principle Analysis:

1. Added gesture processing after initialization:

2. accurately calculate the three statuses of gestures

3. uilabel comes with highlightedtextcolor :)

The principle is so simple :)

 

 

 

 

 

 

 

 

 

A uilabel that triggers a click event and changes color

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.