IOS uses runtime to modify the placeholder text color of the text box (TextField) _ios

Source: Internet
Author: User

Objective

In our usual development, sometimes encounter background darker interface needs to add Textfield,textfield the default placeholder color is also dark gray, more difficult to see, this time need to modify the booth text color, but the system does not provide the corresponding method, then we need to customize the

Modified effect

Words not much said, the following code:

. h file

#import <UIKit/UIKit.h>

@interface uitextfield (placeholder)

@property Uicolor * Placeholdercolor;

@end
. m file #import "uitextfield+placeholder.h" #import <objc/message.h> nsstring * Const PLACEHOLDERCOLORNAME = @ "P

Laceholdercolor "; @implementation Uitextfield (Placeholder) + (void) load {//Get Setplaceholder method Setplaceholder = Class_getinstancem
 Ethod (Self, @selector (setplaceholder:));

 Get Bs_setplaceholder Method Bs_setplaceholder = Class_getinstancemethod (self, @selector (bs_setplaceholder:));
Exchange Method Method_exchangeimplementations (Setplaceholder, Bs_setplaceholder); 
 }//OC like lazy load, use the time to load//need to add properties to the system Uitextfield, only use runtime-(void) Setplaceholdercolor: (Uicolor *) Placeholdercolor { Set Association Objc_setassociatedobject (self, (__bridge const void *) (placeholdercolorname), Placeholdercolor, objc_

 Association_retain_nonatomic);

 Set placeholder text color Uilabel *placeholderlabel = [self valueforkeypath:@ "Placeholderlabel"];

Placeholderlabel.textcolor = Placeholdercolor; }-(Uicolor *) Placeholdercolor {//Returns association return Objc_getassociatedobject (self, __bridge const void *) (placeholdercolorname)); //Set placeholder text and set placeholder text color-(void) Bs_setplaceholder: (NSString *) placeholder {//1. Set placeholder text [self Bs_setplaceholder:placehol


 Der];

2. Set placeholder text color self.placeholdercolor = Self.placeholdercolor; } @end

Summarize

The above is iOS use runtime to modify text box placeholder text color All content, hope to develop iOS can help you, if there is doubt you can message exchange.

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.