(turn) Change Uitextfield placeholder color, font, input cursor position, etc.

Source: Internet
Author: User

We sometimes need to customize the style of the Uitextfield object, and can add many different rewriting methods to change the display behavior of the text field. All of these methods return a CGRECT structure, set the boundary range for each part of the text field, and even modify the placeholder color, font.

–textrectforbounds://rewrite to reset text area

–drawtextinrect://Change the Emoji property. When overridden, calling super can be drawn by default drawing properties, so you don't have to call super if you completely override the drawing function.

–placeholderrectforbounds://Override to reset placeholder area

–drawplaceholderinrect://Override to change the draw placeholder property. Calling super When overridden can be drawn by default drawing properties, and you don't have to call super if you completely override the drawing function.

–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 the picture of the button to be distorted

–leftviewrectforbounds:

–rightviewrectforbounds:

By –drawplaceholderinrect: Method can change placeholder color, font, see Code:

First define a class Customtextfield let it inherit Uitextfield implement the following methods:

Control the location 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);

}

Control the position of the placeholder, left and right 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 understand

return inset;

}

Control where text is displayed

-(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 understand

return inset;

}

Control where text is edited

-(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];

}

Here is the code that uses Customtextfield, which can be placed in viewdidload and other methods

_textfield = [[Customtextfield alloc] Initwithframe:cgrectmake (20, 150, 280, 30)];

_textfield.placeholder = @ "Please 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];

(turn) Change Uitextfield placeholder color, font, input cursor position, etc.

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.