1. Create
01. uitextfield * mytextfield = [[uitextfield alloc] initwithframe: cgrectmake (50,100,200, 50)];
2. Set Delegation
01. mytextfield. Delegate = self; // The delegate class must comply with the uitextfielddelegate protocol.
3. Setting the uicontrol attribute is fully usable for uitextfield. The following are all attributes extended by uitextfield.
01. mytextfield. textalignment = uitextalignmentleft; // The default value is left alignment. This is the extended property of uitextfield.
02. mytextfield. borderstyle = uitextborderstylebezel; // no border is displayed by default. If a custom background image border is used, the border is ignored.
03. mytextfield. placeholder = @ "enter your account here"; // draw a gray string as a placeholder for the blank text field
04. mytextfield. clearsonbeginediting = yes; // set to yes. When a text field is clicked, the field content is cleared.
05. mytextfield. adjustsfontsizetofitwidth = yes; // when set to yes, the text will be automatically reduced to adapt to the text window size. By default, the original size is kept, and long text is rolled.
06. // mytextfield. Background = [uiimage imagenamed: @ "registbtn"]; // you can accept the uiimage object. This setting invalidates the border.
07. mytextfield. clearbuttonmode = uitextfieldviewmodeunlessediting; // The 'X' Clear button displayed on the right
08. // mytextfield. leftview =
09. // mytextfield. leftviewmode =
10. // mytextfield. rightview =
11. // mytextfield. rightviewmode =
4. you can override the following method when creating a uitextfield subclass: borderrectforbounds specify the border of the rectangle textrectforbounds specify the border of the displayed text specify the border of the standing text editingrectforbounds specify the border of the text in editing clearbuttonrectforbounds specify the border of the displayed clear button specify shows the delegate method of the right-attached view.
==========================================================
01.-(cgrect) clearbuttonforbounds :( cgrect) bounds {
02. Return cgrectmake (bounds. Origin. x + bounds. Size. Width-50,
03. bounds. Origin. Y + bounds. Size. Height-20, 16, 16 );
04 .}
==========================================================
01.-(bool) textfieldshouldbeginediting :( uitextfield *) textfield {
02. // return a bool value and specify whether to edit the text field in sequence.
03. Return yes;
04 .}
==========================================================
01.-(void) textfielddidbeginediting :( uitextfield *) textfield {
02. // triggered when editing starts. The text field will become first responder
03 .}
==========================================================
01.-(bool) textfieldshouldendediting :( uitextfield *) textfield {
02. // return the bool value, specifying whether to allow end editing of text fields. When the editing is complete, the text field will give way to the first responder
03. // if you want to stop the text field from disappearing when the user finishes editing, you can return no
04. // This is useful for some programs whose text fields must always be active, such as instant messages.
05. Return no;
06 .}
==========================================================
01.-(bool) textfield :( uitextfield *) textfield shouldchangecharactersinrange :( nsange) range replacementstring :( nsstring *) string {
02. // this method is called when you use the automatic correction function to change the input text to the recommended text.
03. // This is especially useful for applications that want to add revocation options
04. // you can trace the last modification made in the field, or make a log record for All edits for auditing purposes.
05. // No can be returned to prevent text changes
06. // The parameter of this method has a nsange object, indicating the location of the text to be changed. The recommended text is also in it.
07. Return yes;
08 .}
==========================================================
01.-(bool) textfieldshouldclear :( uitextfield *) textfield {
02. // return a bool value to indicate whether content can be cleared based on user requests
03. // you can set the condition to allow content to be cleared.
04. Return yes;
05 .}
==========================================================
01.-(bool) textfieldshouldreturn :( uitextfield *) textfield {
02. // return a bool value indicating whether to allow editing to end when you press the Enter key.
03. // if you are allowed to call the resignfirstresponder method, this will end the editing, and the keyboard will be collapsed.
04. [textfield resignfirstresponder]; // check the meaning of the resign to understand this method.
05. Return yes;
06 .}
==========================================================
Solution to uitextfield blocking by the virtual keyboard:
In rootviewcontroller. h:
# Import <uikit/uikit. h>
@ Interface rootviewcontroller: uiviewcontroller <uitextfielddelegate> {
Uitextfield * textfield1;
Uitextfield * textfield2;
}
@ Property (nonatomic, retain) uitextfield * textfield1;
@ Property (nonatomic, retain) uitextfield * textfield2;
-(Ibaction) backgroundtap :( ID) sender;
@ End
In rootviewcontroller. M:
# Import "rootviewcontroller. H"
@ Implementation rootviewcontroller
@ Synthesize textfield1;
@ Synthesize textfield2;
// The designated initializer. Override if you create the Controller programmatically and want to perform customization that is not appropriate for viewdidload.
/*
-(ID) initwithnibname :( nsstring *) nibnameornil bundle :( nsbundle *) nibbundleornil {
Self = [Super initwithnibname: nibnameornil Bundle: nibbundleornil];
If (Self ){
// Custom initialization.
}
Return self;
}
*/
/*
// Implement loadview to create a view hierarchy programmatically, without using a nib.
-(Void) loadview {
Uiview * back = [[uiview alloc] initwithframe: [uiscreen mainscreen] bounds];
Back. backgroundcolor = [uicolor graycolor];
Self. view = back;
[Back release];
}
*/
// Implement viewdidload to do additional setup after loading the view, typically from a nib.
-(Void) viewdidload {
[Super viewdidload];
Uicontrol * _ back = [[uicontrol alloc] initwithframe: Self. View. Frame];
_ Back. backgroundcolor = [uicolor graycolor];
Self. view = _ back;
[_ Back release];
[(Uicontrol *) self. View addtarget: Self action: @ selector (backgroundtap :) forcontrolevents: uicontroleventtouchdown];
Textfield1 = [[uitextfield alloc] initwithframe: cgrectmake (20,300,200, 30)];
Textfield1.backgroundcolor = [uicolor clearcolor];
Textfield1.borderstyle = uitextborderstyleroundedrect;
Textfield1.textcolor = [uicolor redcolor];
Textfield1.delegate = self;
[Self. View addsubview: textfield1];
Textfield2 = [[uitextfield alloc] initwithframe: cgrectmake (20, 30,200, 30)];
Textfield2.backgroundcolor = [uicolor clearcolor];
Textfield2.borderstyle = uitextborderstyleroundedrect;
Textfield2.textcolor = [uicolor redcolor];
Textfield2.delegate = self;
[Self. View addsubview: textfield2];
}
# Pragma mark-
# Pragma mark solution for uitextfield blocking by virtual keyboard
-(Void) keyboardwillshow :( nsnotification *) noti
{
// Adjust the keyboard input interface
// Keyboard height
Float heights = 216.0;
Cgrect frame = self. View. frame;
Frame. size = cgsizemake (frame. Size. Width, frame. Size. Height-height );
[Uiview beginanimations: @ "curl" context: Nil]; // starts the animation.
[Uiview setanimationduration: 0.30];
[Uiview setanimationdelegate: Self];
[Self. View setframe: frame];
[Uiview commitanimations];
}
-(Bool) textfieldshouldreturn :( uitextfield *) textfield
{
// When the user presses return, take focus away from the text field so that the keyboard is dismissed.
Nstimeinterval animationduration = 0.30f;
[Uiview beginanimations: @ "resizeforkeyboard" context: Nil];
[Uiview setanimationduration: animationduration];
Cgrect rect = cgrectmake (0.0f, 20366f, self. View. Frame. Size. Width, self. View. Frame. Size. Height );
Self. View. Frame = rect;
[Uiview commitanimations];
[Textfield resignfirstresponder];
Return yes;
}
-(Void) textfielddidbeginediting :( uitextfield *) textfield
{
Cgrect frame = textfield. frame;
Int offset = frame. Origin. Y + 32-(self. View. Frame. Size. Height-216.0); // The keyboard height is 216.
Nstimeinterval animationduration = 0.30f;
[Uiview beginanimations: @ "resizeforkeyboard" context: Nil];
[Uiview setanimationduration: animationduration];
Float width = self. View. Frame. Size. width;
Float Height = self. View. Frame. Size. height;
If (Offset> 0)
{
Cgrect rect = cgrectmake (0.0f,-offset, width, height );
Self. View. Frame = rect;
}
[Uiview commitanimations];
}
# Pragma mark-
# Pragma mark touch the background to close the virtual keyboard
-(Ibaction) backgroundtap :( ID) sender
{
// When the user presses return, take focus away from the text field so that the keyboard is dismissed.
Nstimeinterval animationduration = 0.30f;
[Uiview beginanimations: @ "resizeforkeyboard" context: Nil];
[Uiview setanimationduration: animationduration];
Cgrect rect = cgrectmake (0.0f, 20366f, self. View. Frame. Size. Width, self. View. Frame. Size. Height );
Self. View. Frame = rect;
[Uiview commitanimations];
[Textfield1 resignfirstresponder];
[Textfield2 resignfirstresponder];
}
# Pragma mark-
/*
// Override to allow orientations other than the default portrait orientation.
-(Bool) shouldautorotatetointerfaceorientation :( uiinterfaceorientation) interfaceorientation {
// Return YES For supported orientations.
Return (interfaceorientation = uiinterfaceorientationportrait );
}
*/
-(Void) didreceivememorywarning {
// Releases the view if it doesn't have a superview.
[Super didreceivememorywarning];
// Release any cached data, images, etc. That aren't in use.
}
-(Void) viewdidunload {
[Super viewdidunload];
// Release any retained subviews of the main view.
// E.g. Self. myoutlet = nil;
}
-(Void) dealloc {
[Textfield1 release];
[Textfield2 release];
[Super dealloc];
}
Backgroundtap in rootviewcontroller. M: method used to touch the background and disable the virtual keyboard.
When using this method, first change the view on rootviewcontroller to uicontrol, and then trigger the above method backgroundtap through the uicontrol event uicontroleventtouchdown :.
Note the following code:
Uicontrol * _ back = [[uicontrol alloc] initwithframe: Self. View. Frame];
_ Back. backgroundcolor = [uicolor graycolor];
Self. view = _ back;
[_ Back release];
[(Uicontrol *) self. View addtarget: Self action: @ selector (backgroundtap :) forcontrolevents: uicontroleventtouchdown];
There are three methods to solve the problem that textfield is blocked by the keyboard:
-(Void) keyboardwillshow :( nsnotification *) noti; // adjust the relationship between the virtual keyboard and self. View.
-(Bool) textfieldshouldreturn :( uitextfield *) textfield; // disable the virtual keyboard when the return key is on the touchkey.
-(Void) textfielddidbeginediting :( uitextfield *) textfield; // when editing text, if the virtual keyboard blocks textfield, the entire view will move up. The moving range is a keyboard height of 216.
References:
(1) http://bbs.chinaunix.net/thread-3679125-1-1.html
(2) http://yang152412.blog.163.com/blog/static/1758615822012284143183/