Original address: http://blog.csdn.net/leeyehong_self/article/details/7939147
We sometimes need to customize the style of the Uitextfield object, and we can add many different rewriting methods to change the display behavior of the text field. These methods return a CGRECT structure, define the bounds of each part of the text field, and even modify the placeholder color, font.
–textrectforbounds://Rewrite to reset the text area
–drawtextinrect://Change the drawing text properties. When overridden, call super can be drawn by default graphic properties, and if you completely rewrite the drawing function, you don't have to call super.
–placeholderrectforbounds://Overrides to reset the placeholder area
–drawplaceholderinrect://Rewrite to change the drawing placeholder properties. When overridden, call super can be drawn by default graphic properties, and if you completely rewrite the drawing function, you don't have to call super.
–borderrectforbounds://rewrite to reset Edge area
–editingrectforbounds://Rewrite to reset edit area
–clearbuttonrectforbounds://Rewrite to reset the Clearbutton position, changing size may cause the button's picture to be distorted
–leftviewrectforbounds:
–rightviewrectforbounds:
Through –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 purge 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, shrink to about 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 editing text is located
-(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 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 placeholder Color, font
-(void) Drawplaceholderinrect: (cgrect) rect
{
Cgcontextref context = Uigraphicsgetcurrentcontext ();
Cgcontextsetfillcolorwithcolor (context, [Uicolor Yellowcolor]. Cgcolor);
[[Uicolor Orangecolor] setfill];
[[Self placeholder] drawinrect:rectwithfont:[uifont systemfontofsize:20]];
}
Here is the code using 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];