[BS-19] 5 ways to change the color of placeholder text in Uitextfield

Source: Internet
Author: User

5 ways to change the placeholder text color of Uitextfield The goal is to have multiple uitextfield on a page, and when the user focuses on a textfield, the placeholder text of the text box turns gray to white. When the text box loses focus, the placeholder color changes back from white to gray.

1. Place Uilabel

The simplest and most stupid method is to put a uilabel in each TextField to act as placeholder, and when the TextField is focused, the placeholder text turns gray to white, and then from white to gray when it loses focus. This method requires a drag line for each uilabel and TextField, by listening to the keyboard notification or Uitextfield agent to learn whether TextField is focused, and then write a bunch of if statements to judge.

2. Modify the Textfield.attributedplaceholder property

Change the placeholder color by nsattributedstring . The trouble with this approach is that you need to know when all the TextField on the page are focused and when it's out of focus (there are two ways:A. listen to the keyboard pop-up notification B. through Uitextfield Agent method Textfielddidbeginediting), and then decide which is white and which is gray. If there are many TextField on the page, you need to write a lot of if statements.

nsmutableattributedstring *attrstring = [[nsmutableattributedstring alloc]initwithstring:@ " phone number " Attributes:@{nsforegroundcolorattributename:[uicolor Whitecolor]}];

self.phoneTextField.attributedPlaceholder = attrstring;

3. Using subclasses of Uitextfield rewriteDrawplaceholderinrect: Method

See code for specific implementations:

//The advantage of this method is that you do not need to get the TextField reference in the Xib, instead of dragging the line, you can change the TextField class of Xib to a custom class directly. After any TextField want to change the color of placeholder, directly defined as the class can be. But you still need to monitor whether the Uitextfield is focused. #import "WZCustomPlaceholderTextField.h"@implementationWzcustomplaceholdertextfield/** @interface nsstring (nsstringdrawing) NSString classification-(cgsize) Sizewithattributes: (Nullable nsdictionary< NSString *, id> *) attrs ns_available (10_0, 7_0); -(void) Drawatpoint: (cgpoint) point withattributes: (Nullable nsdictionary<nsstring *, id> *) Attrs ns_available ( 10_0, 7_0); -(void) Drawinrect: (cgrect) Rect withattributes: (Nullable nsdictionary<nsstring *, id> *) attrs ns_available (10_0 , 7_0); @end*/- (void) Drawplaceholderinrect: (cgrect) Rect {//rect represents the Frame/rect of the custom TextField//because placeholder belongs to the NSString type, all NSString have drawinrect methods, but this method appears to be valid only in the Draw Start method[Self.placeholder Drawinrect:cgrectmake (0,Ten, Rect.size.width, Rect.size.height) withattributes:@{                                                                                                       Nsforegroundcolorattributename:[uicolor Whitecolor], Nsfontattributename:[uifont systemfontofsize: the]                                                                                                       }]; }- (void) DrawRect: (cgrect) Rect {//rect represents the Frame/rect of the custom TextField[Super Drawrect:rect];//call the DrawRect method of the parent class Uitextfield and pass the custom dimension in. The parent class must be called  }@end
4. use Uitextfield subclasses to rewrite the drawrect: Method, in DrawRect [Self.placeholder Drawinrect:] to set. 5. Set a value _placeholderlabel the internal instance variable hidden by KVC to Uitextfield.

Using runtime to print out all the instance variables of Uitextfield, we found an internal instance variable called _placeholderlabel, and set the value through KVC in the custom class. The advantage of this approach is that it is easy to set values with KVC and can be written in any location. In this example we rewrite the Becomefirstresponder: and resignfirstresponder methods to monitor whether the Uitextfield is focused and then use KVC to set the value.

//WZTEXTFIELD.M//using runtime to print out all the instance variables of Uitextfield, we found an internal instance variable called _placeholderlabel, and set the value through KVC in the custom class. //#import "WZTextField.h"@implementationWztextfield//Call Order 1 (called when created from Xib)-(Instancetype) Initwithcoder: (Nscoder *) Adecoder {if(self =[Super Initwithcoder:adecoder]) {    }    returnSelf ;}//Call order 2 (called when created from Xib)- (void) awakefromnib {}//Call Order 3 (either Xib or pure code creation will be called)- (void) DrawRect: (cgrect) rect {[Super Drawrect:rect]; Self.tintcolor= Self.textcolor;//set the cursor color to be the same as the text color}-(BOOL) becomefirstresponder {[self setvalue:[uicolor whitecolor] Forkeypath:@"_placeholderlabel.textcolor"]; return[Super Becomefirstresponder];}-(BOOL) resignfirstresponder {[self setvalue:[uicolor graycolor] Forkeypath:@"_placeholderlabel.textcolor"]; return[Super Resignfirstresponder];}@end

[BS-19] 5 ways to change the color of placeholder text in Uitextfield

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.