Uitextfield is a very common control in iOS.Used to receive user input,Complete application and user interaction.Its main properties are set as follows:

// Initialize the textfield and set the position and size
 uitextfield * text=[[uitextfield alloc] initwithframe:cgrectmake (20, 20, 130, 30)];
// Set the border style,Border style will only be displayed if set
 typedef enum {
 uitextborderstylenone, uitextborderstyleline, uitextborderstylebezel, uitextborderstyleroundedrect
 } uitextborderstyle;
// Set the background color of the input box,Set it to white at this time. If a custom background image border is used, it will be ignored
 text.backgroundcolor=[uicolor whitecolor];
// Set the background
 text.background=[uiimage imagenamed:@"dd.png"];
// Set the background
 text.disabledbackground=[uiimage imagenamed:@"cc.png"];
// When the input box has no content,Watermark Prompt Prompt content is password
 text.placeholder [email protected]"password";
// Set the font style and size of the input box content
 text.font=[uifont fontwithname:@"arial" size:20.0f];
// Set font color
 text.textcolor=[uicolor redcolor];
// Is there a cross in the input box,At what time
Used to delete the contents of the input box at one time
typedef enum {
 uitextfieldviewmodenever, not appearing again
 uitextfieldviewmodewhileediting, appearing while editing
 uitextfieldviewmodeunlessediting, except for editing
 uitextfieldviewmodealways keep appearing
} uitextfieldviewmode;
// The text from the beginning of the input box
 text.text [email protected]"the text in the input box from the beginning";
// It becomes a dot for every character input
// whether error correction
typedef enum {
 uitextautocorrectiontypedefault, default
 uitextautocorrectiontypeno, no automatic error correction
} uitextautocorrectiontype;
// Empty again after editing
// Content alignment
// The vertical alignment of the content uitextfield inherits from uicontrol, there is a property contentverticalalignment in this class
// When set to yes, the text will automatically shrink to fit the text window size.
The default is to keep the original size,And let long text scroll
// Set the minimum font size for automatic reduction display
// Set the style of the keyboard
typedef enum {
 uikeyboardtypedefault, the default keyboard,Support all characters
 uikeyboardtypeasciicapable, support ascii's default keyboard
 uikeyboardtypenumbersandpunctuation, standard phone keypad,Support + * #characters
 uikeyboardtypeurl, url keyboard, supports .com button, only supports url characters
uikeyboardtypenumberpad, number pad
uikeyboardtypephonepad, phone keypad
 uikeyboardtypenamephonepad, phone keypad,Also supports entering names
uikeyboardtypeemailaddress, a keyboard for entering email addresses
uikeyboardtypedecimalpad, numeric keyboard with numbers and decimal points
 uikeyboardtypetwitter, optimized keyboard,Convenient input of @, #characters
 uikeyboardtypealphabet=uikeyboardtypeasciicapable,} uikeyboardtype;
// whether the first letter is uppercase
typedef enum {
 uitextautocapitalizationtypenone, not automatically capitalized
 uitextautocapitalizationtypewords, capitalize
 uitextautocapitalizationtypesentences, sentence capitalization
 uitextautocapitalizationtypeallcharacters, all letters are capitalized
} uitextautocapitalizationtype;
// return key becomes what key
typedef enum {
 uireturnkeydefault, default gray button,Marked return
 uireturnkeygo, blue button labeled go
 uireturnkeygoogle,Blue button labeled google,Term search
 uireturnkeyjoin,Blue button labeled join
 uireturnkeynext,Blue button labeled next
 uireturnkeyroute,Blue button labeled route
 uireturnkeysearch,Blue button labeled search
 uireturnkeysend,Blue button labeled send
 uireturnkeyyahoo,Blue button labeled yahoo
 uireturnkeyyahoo,Blue button labeled yahoo
 uireturnkeyemergencycall, emergency call button
} uireturnkeytype;
// Keyboard appearance
typedef enum {
uikeyboardappearancedefault, the default appearance,light grey
uikeyboardappearancealert, dark gray graphite
} uireturnkeytype;
// Set proxy for implementing the protocol
// Add textfield to the view
 [self.window addsubview:text];
// The picture on the far right is the following code, similar on the left
 uiimageview * image=[[uiimageview alloc] initwithimage:[uiimage imagenamed:@"right.png"]];
typedef enum {
 uitextfieldviewmodenever, uitextfieldviewmodewhileediting, uitextfieldviewmodeunlessediting, uitextfieldviewmodealways
} uitextfieldviewmode;
// Press the return key to close the keyboard to become firstresponder
Classes should use the uitextfielddelegate protocol
text.delegate=self;declares that the proxy for text is me,I will implement the method of retracting the keyboard. This method is in uitextfielddelegate, so we will use the uitextfielddelegate protocol
-(bool) textfieldshouldreturn:(uitextfield *) textfield
 [text resignfirstresponder];// mainly where [receiver resignfirstresponder] can be called to receive the keyboard corresponding to receiver
 return yes;
Overriding drawing behavior
In addition to the style options of the uitextfield object,You can also customize the uitextfield object, add many different overriding methods for it,To change the display behavior of the text field.
These methods will return a cgrect structure, which specifies the bounds of each part of the text field.
The following methods can be overridden.
– Textrectforbounds:// Rewrite to reset text area
– Drawtextinrect:
// Change the emoji properties.
Calling super when rewriting can draw according to the default graphics properties,If you completely rewrite the drawing function yourself,No need to call super.
– Placeholderrectforbounds:
// Override to reset the placeholder area
– Drawplaceholderinrect:
// Override changes drawing placeholder properties.
Calling super when rewriting can draw according to the default graphics properties,If you completely rewrite the drawing function yourself,No need to call super.
– Borderrectforbounds:
// Override to reset the edge area
– Editingrectforbounds:
// Rewrite to reset the editing area
– Clearbuttonrectforbounds:
// Override to reset the clearbutton position, changing the size may cause the picture of the button to be distorted
– Leftviewrectforbounds:
– Rightviewrectforbounds:
Delegate method
-(bool) textfieldshouldbeginediting:(uitextfield *) textfield {
// Return a bool value, specify whether to start editing the sequential text field
 return yes;
-(void) textfielddidbeginediting:(uitextfield *) textfield {
 // Triggered when editing starts,The text field will become the first responder
-(bool) textfieldshouldendediting:(uitextfield *) textfield {
// Return the bool value, specify whether to allow the text field to finish editing,When editing is finished,Text field will give first responder
// To prevent the text field from disappearing when the user finishes editing,Can return no
// This is useful for programs where text fields must always be active,Instant messaging
 return no;
-(bool) textfield:(uitextfield *) textfield shouldchangecharactersinrange:(nsrange) range replacementstring:(nsstring *) string {
// When the user uses the auto correction function,When changing the input text to the recommended text,This method will be called.
// This is especially useful for applications that want to include the undo option
// can track the last modification made in the field,You can also log all edits,For auditing purposes.
// To prevent the text from being changed, you can return no
// The parameter of this method has an nsrange object that indicates the position of the changed text.
Proposed text changes are also included
  return yes;
-(bool) textfieldshouldclear:(uitextfield *) textfield {
// Return a bool value indicating whether to allow clearing content based on user requests
// Can be set to clear content only under certain conditions
  return yes;
-(bool) textfieldshouldreturn:(uitextfield *) textfield {
// Return a bool value indicating whether to allow editing to end when the enter key is pressed
// If it is allowed to call the resignfirstresponder method, this time ends the editing,The keyboard will be retracted [textfield resignfirstresponder];
// Check the meaning of the word resign to understand this method
  return yes;
  • Previous JS form validation examples explain
  • Next Ruby string and array maximization discussion