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.