Change the color and font of uitextfield placeholder

Source: Internet
Author: User

Change the color and font of uitextfield placeholder

 

Sometimes we need to customize the uitextfield object style. We can add many different rewriting methods to change the display behavior of text fields. These methods return a cgrect structure, define the boundary range of each part of the text field, and even modify the placeholder color and font.

 

-Textrectforbounds: // rewrite to reset the text area

-Drawtextinrect: // modify the painting text attributes. When rewriting, you can call super to draw based on the default graphic attributes. If you completely rewrite the painting function, you do not need to call super.

-Placeholderrectforbounds: // rewrite to reset the placeholder Area

-Drawplaceholderinrect

-Borderrectforbounds: // rewrite to reset the Edge Area

-Editingrectforbounds: // rewrite to reset the editing area

-Clearbuttonrectforbounds: // rewrite to reset the clearbutton position. Changing the size may cause distortion of the button image.

-Leftviewrectforbounds:

-Rightviewrectforbounds:

 

-Drawplaceholderinrect: The color and font of a placeholder can be changed. See the Code:

First, define a class customtextfield to inherit uitextfield and implement the following methods:

// Control the position of the clear button

-(Cgrect) clearbuttonrectforbounds :( cgrect) bounds

{

Return cgrectmake (bounds. Origin. x + bounds. Size. Width-50, bounds. Origin. Y + bounds. Size. Height-20, 16, 16 );

}

 

// Controls the position of the placeholder, which is reduced from left to right by 20

-(Cgrect) placeholderrectforbounds :( cgrect) bounds

{

// Return cgrectinset (bounds, 20, 0 );

Cgrect inset = cgrectmake (bounds. Origin. x + 100, bounds. Origin. Y, bounds. Size. Width-10, bounds. Size. Height); // better understanding

Return inset;

}

// Control the position of the displayed text

-(Cgrect) textrectforbounds :( cgrect) bounds

{

// Return cgrectinset (bounds, 50, 0 );

Cgrect inset = cgrectmake (bounds. Origin. x + 190, bounds. Origin. Y, bounds. Size. Width-10, bounds. Size. Height); // better understanding

Return inset;

 

}

// Control the location of the edited text

-(Cgrect) editingrectforbounds :( cgrect) bounds

{

// Return cgrectinset (bounds, 10, 0 );

Cgrect inset = cgrectmake (bounds. Origin. x + 10, bounds. Origin. Y, bounds. Size. Width-10, bounds. Size. Height );

Return inset;

}

// Control the Left view position

-(Cgrect) leftviewrectforbounds :( cgrect) bounds

{

Cgrect inset = cgrectmake (bounds. Origin. x + 10, bounds. Origin. Y, bounds. Size. Width-250, bounds. Size. Height );

Return inset;

// Return cgrectinset (bounds, 50, 0 );

}

 

// Control the color and font of placeholder

-(Void) drawplaceholderinrect :( cgrect) rect

{

// Cgcontextref context = uigraphicsgetcurrentcontext ();

// Cgcontextsetfillcolorwithcolor (context, [uicolor yellowcolor]. cgcolor );

[[Uicolororangecolor] setfill];

[[Selfplaceholder] drawinrect: rectwithfont: [uifontsystemfontofsize: 20];

}

 

 

// The following code uses customtextfield, which can be placed in methods such as viewdidload.

_ Textfield = [[customtextfield alloc] initwithframe: cgrectmake (20,150,280, 30)];

_ Textfield. placeholder = @ "enter account information ";

_ Textfield. borderstyle = uitextborderstyleroundedrect;

_ Textfield. textalignment = uitextalignmentleft;

_ Textfield. Delegate = self;

_ Textfield. clearbuttonmode = uitextfieldviewmodewhileediting;

_ Textfield. Text = @ "AA ";

Uiimageview * imgv = [[uiimageview alloc] initwithimage: [uiimage imagenamed: @ "icon-iwant-2.png"];

_ Textfield. leftview = imgv;

_ Textfield. leftviewmode = uitextfieldviewmodealways;

[Self. View addsubview: _ textfield];

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.