Initialize TextField and set location and size
Uitextfield *text = [[[Uitextfield Alloc]initwithframe:cgrectmake]]; //set border style, Border style is only displayed if set Text.borderstyle = uitextborderstyleroundedrect; typedef enum { uitextborderstylenone, uitextborderstyleline, Uitextborderstylebezel, Uitextborderstyleroundedrect } uitextborderstyle; //set the background color of the input box, at which time it is set to white If you use a custom background picture border will be ignored Text.backgroundcolor = [Uicolor whitecolor]; //set Background Text.background = [UIImage imagenamed:@ "Dd.png"]; //set background Text.disabledbackground = [UIImage imagenamed:@ "Cc.png"]; //when the input box does not have content, the watermark hint tip content is password Text.placeholder = @ "password"; //sets the font style and size of the input box contents Text.font = [Uifont fontwithname:@] Arial "size:20.0f]; //set font color text.textcolor = [Uicolor redcolor]; //If there is a fork in the input box, when it is displayed, to delete the contents of the input box at once Text.clearbuttonmode = uitextfieldviewmodealways; typedef enum { Uitextfieldviewmodenever, heavy non-appearing uitextfieldviewmodewhileediting, uitextfieldviewmodeunlessediting when editing, except for editing, it appears Uitextfieldviewmodealways always appears } uitextfieldviewmode; //the input box starts with the text Text.text = @ "The text of the input box at the beginning"; //every character you enter into points Password input text.securetextentry = YES ; //Error correction Text.autocorrectiontype = uitextautocorrectiontypeno; typedef enum { uitextautocorrectiontypedefault, default Uitextautocorrectiontypeno, no automatic error correction Uitextautocorrectiontypeyes, automatic error correction } uitextautocorrectiontype; //re-edit to clear text.clearsonbeginediting = yes; //Content Alignment text.textalignment = Vertical alignment of uitextalignmentleft; //content Uitextfield inherit from Uicontrol, There is an attribute in this class contentverticalalignment text.contentverticalalignment = When uicontrolcontentverticalalignmentcenter; //is set to Yes, the text automatically shrinks to fit the text window size. The default is to keep the original size and let long text scroll Textfied.adjustsfontsizetofitwidth = yes; //Set the minimum font size for automatic zoom out Text.minimumfontsize = 20; //Set the keyboard style text.keyboardtype = Uikeyboardtypenumberpad; typedef enum { Uikeyboardtypedefault, default keyboard, all characters supported uikeyboardtypeasciicapable, ASCII-enabled default keyboard Uikeyboardtypenumbersandpunctuation, standard telephone keypad, support +*# characters uikeyboardtypeurl, URL keypad, support for. com buttons only URL characters UIKeyboardTypeNumberPad, Numeric keypad uikeyboardtypephonepad, telephone keypad UIKEYBOARDTYPENAMEPHonepad, telephone keypad, also supports input person name uikeyboardtypeemailaddress, keyboard for entering electronic e-mail addresses Uikeyboardtypedecimalpad, numeric keypad with numbers and decimal points uikeyboardtypetwitter, Optimized keypad for easy input @, #字符 Uikeyboardtypealphabet = uikeyboardtypeasciicapable, } uikeyboardtype //First Letter Capital Text.autocapitalizationtype = uitextautocapitalizationtypenone; typedef enum { uitextautocapitalizationtypenone, not automatically capitalized Uitextautocapitalizationtypewords, capitalize the first letter of the word uitextautocapitalizationtypesentences, capitalize the first letter of the sentence uitextautocapitalizationtypeallcharacters, All letters are capitalized } Uitextautocapitalizationtype; //return key into what key text.returnkeytype =uireturnkeydone; typedef enum { uireturnkeydefault, Default Gray button, labeled return uireturnkeygo, Blue button labeled Go uireturnkeygoogle, blue button labeled Google, search uireturnkeyjoin, blue button labeled Join Uireturnkeynext, blue button with 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 textview.keyboardappearance= Uikeyboardappearancedefault; typedef enum { uikeyboardappearancedefault, default appearance, light grey Uikeyboardappearancealert, dark grey graphite } uireturnkeytype; //set Agent for implementing protocol Text.delegate = self; //Adding TextField to the view [Self.window addsubview:text]; //The rightmost picture is the following code similar to the left Uiimageview *image=[[uiimageview Alloc] Initwithimage:[uiImage imagenamed:@ "Right.png"]]; text.rightview=image; Text.rightviewmode = uitextfieldviewmodealways; typedef enum { Uitextfieldviewmodenever, uitextfieldviewmodewhileediting, uitextfieldviewmodeunlessediting, UITEXTFIELDVIEWMODEALWAYS } uitextfieldviewmode; //Press RETURN key to go down becomefirstresponder class to adopt uitextfielddelegate protocol text.delegate = self; Declare the text of the agent is me, I will go to achieve the keyboard down the method This method in uitextfielddelegate so we have to adopt uitextfielddelegate this protocol -( BOOL) Textfieldshouldreturn: (Uitextfield *) textfield { [Text resignfirstresponder]; //main [receiver Resignfirstresponder] where to call the receiver corresponding to the keyboard down
return yes;
} rewrite the paint behavior , in addition to Uitextfield object style options, you can also customize the Uitextfield object, add many different rewrite methods for him, To change the display behavior of the text field. All of these methods return a CGRECT structure that has a boundary range for each part of the text field. The following methods can be overridden. – textrectforbounds: //rewrite to reset text area – drawtextinrect: // Changes the emoji 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. – placeholderrectforbounds://Override to reset placeholder area – drawplaceholderinrect://Override to change the draw placeholder property. When overridden, calling super can be drawn by default drawing properties, and you don't have to call super if you completely override the drawing function . – Borderrectforbounds://Override to reset Edge area – editingrectforbounds://Override to reset edit area – Clearbuttonrectforbounds://Rewrite 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{ //Returns a bool value specifying whether the sequential text field starts editing return yes; } - (void) textfielddidbeginediting: (uitextfield *) TextField{ //is triggered when editing starts, the text field becomes First responder } - (BOOL) Textfieldshouldendediting: (uitextfield *) textfield{ //Returns a bool value that specifies whether the text field is allowed to end editing, and when the edit is finished, the text field will yield first responder //To prevent a text field from disappearing when the user finishes editing, you can return no //This is useful for programs that must remain active for some text fields, such as instant messaging return no; } - (BOOL) TextField: (uitextfield*) Textfield shouldchangecharactersinrange: ( Nsrange) Range replacementstring: (nsstring *) string{ //This method is called when the user uses the AutoCorrect feature to modify the input text to the recommended text. //This is especially useful for applications that want to join the Undo option //can track the last modification made in a field, or log records of all edits for auditing purposes. //to prevent text from being changed you can return no //the method has a Nsrange object in the parameter, indicating the position of the changed text, and the suggested text is also in it return YES; } - (BOOL) Textfieldshouldclear: (uitextfield *) textfield{ // Returns a bool value indicating whether to allow content to be purged based on user request //can be set under certain conditions to allow clear content return yes; } -(BOOL) Textfieldshouldreturn: (uitextfield *) textfield{ //Returns a BOOL value indicating whether the edit is allowed to end when the ENTER key is pressed //If you allow the resignfirstresponder method to be called, this causes the end of the edit, and the keyboard is closed [textfield resignfirstresponder]; Check out the meaning of the word resign to understand this method return yes; } notification uitextfield is derived from Uicontrol, so the notification system in the Uicontrol class is also available in the Text field. In addition to the standard events of the Uicontrol class, you can also use the following Uitextfield class-specific events UITextFieldTextDidBeginEditingNotification uitextfieldtextdidchangenotification uitextfieldtextdidendeditingnotification triggered when the text field exits edit mode. The object property of the notification stores the final text. because text fields use the keyboard to enter text, action notifications are also sent when the following events occur uikeyboardwillshownotification// Keyboard display before sending UIKeyboardDidShowNotification //keyboard display after sending uikeyboardwillhidenotification//keyboard hidden before sending UIKeyboardDidHideNotification //keyboard is hidden after sending 1, text : Sets the default text for the text box.  2, placeholder : can display gray words in a text box to indicate what the user should enter in the text box. When the data is entered in this text box, the gray word used for the hint will automatically disappear. 3, background : 4, disabled : If this is selected, the user will not be able to change the contents of the text box. 5, followed by three buttons, is used to set the alignment. 6, Border style : Select the border style. 7, clear button : This is a drop-down menu, you can choose to clear when the button appears, the so-called clear button is out of a text box now to the right of the small x , you can have the following options: 7.1 never appears : never appear 7.2 appears while editing : when editing occurs & nbsp 7.3 appears unless editing : 7.4 are always visible : visible 8, Clear When editing begins : is selected, the previous content in the text box is cleared when you start editing the text box. For example, you are now in this text box A entered "What"  , and then to edit the text box b, if you come back to edit the text box a , then the "what" will be cleared immediately. 9, text color : sets the color of the text in the text box.  10, font : sets the font and font size of the text. 11, min font size : set the minimum font that the text box can display (but I don't feel It) 12, Adjust to fit : specifies that when the size of the text box is reduced, Whether the text in the text box also shrinks. Select it to make all text visible, even if the text is long. However, this option should be used in conjunction with min font size , and the text will not be smaller than the set min font size . The next partTo set how the keyboard is displayed.  13, captitalization : set uppercase. There are four options in the drop-down menu: 13.1 none : do not set caps 13.2 words : capitalize each word first letter, The words here refer to strings separated by spaces 13.3 sentances : The first letter of each sentence, where the sentence is a string separated by a period and a space 13.4 All characters : so the letter capitalization 14, correction : check spelling, default is YES . 15, keyboard : Select keyboard types, such as full numbers, letters, and numbers. 16, appearance: 17, return key : Select the Return key, you can select Search , Return , done and so on. 18, auto-enable return key : If selected, the keyboard's return key is valid only if at least one character is entered in the text box. 19, secure : When your text box is used as a password input box, you can select this option, at which point the characters are displayed as asterisks. 1.alignment horizontal Horizontal Alignment 2.alignment vertical vertical alignment 3. Used to return a bool value Whether the input box is selected (checked) Enabled (available) highlighted (highlight) limit only enter specific characters (BOOL) TextField: (Uitextfield *) TextField Shouldchangecharactersinrange: (nsrange) Range replacementstring: (Nsstring *) string{ Nscharacterset *cs; cs = [[Nscharacterset charactersetwithcharactersinstring:numbers]invertedset]; NSString *filtered = [[ String componentsseparatedbycharactersinset:cs]componentsjoinedbystring:@ ""]; //groups separated by CS, array separated by @ "" BOOL canchange = [string isequaltostring:filtered]; Return canchange; } above that NUMBERS is a macro that can be defined at the top of the file: #define NUMBERS @ "0123456789n" (This delegate can enter a number and line break, please note that this n, if you do not write this, the Done button will not be triggered, if used in Searchbar, will not trigger the search event, because you have to restrict the input n, very miserable, I found in the project. So, if you want to restrict the input of English and numbers, you can define this as: #define Kalphanum @ " Abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789″. Of course, you can also do a hint before the above method return, such as prompting the user to enter only numbers and the like. If you feel the need. restrict the input of characters -(BOOL) TextField: (Uitextfield *) to a certain length TextField Shouldchangecharactersinrange: (nsrange) Range replacementstring: (NSString *) STRING;&Nbsp; {//string is the character that is entered at this time textfield is the input box that is being entered at this time returns Yes to change the value of the input box no reverse if ([string isequaltostring:@ "n"])//press passing to change { return yes;& nbsp; } NSString * tobestring = [Textfield.text Stringbyreplacingcharactersinrange:range withstring:string]; //Get the contents of the input box if ( Self.mytextfield = = TextField)//Determine if we want to limit the input box { if ([ Tobestring Length] > { //If the input box content is greater than 20 pop-up warning textfield.text = [tobestring substringtoindex:20];& nbsp; Uialertview *alert = [[Uialertview alloc] Initwithtitle:nil message: @ "More than the maximum number of words can not be entered" Delegate:nil cancelbuttontitle:@ "OK" otherbuttontitles:nil, nil] autorelease]; [alert show]; return no; } } RET Urn yes; }
iOS Development Learning-lesson three Uitextfield using