IOS Uitextfield Control Summary
First declare the following summary is not written by yourself.
Initialize the TextField and set the location and size Uitextfield *text = [[Uitextfield Alloc]initwithframe:cgrectmake]]; Set border style, only set to show border style Text.borderstyle = Uitextborderstyleroundedrect; typedef enum { uitextborderstylenone, uitextborderstyleline, uitextborderstylebezel, Uitextborderstyleroundedrect } uitextborderstyle; Sets the background color of the input box, when set to white if a custom background picture border is ignored Text.backgroundcolor = [uicolor whitecolor]; //settings 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 prompt content is password text.placeholder = @ "password"; // Set font style and size for 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, never appear uitextfieldviewmodewhileediting, edit when uitextfieldviewmodeunlessediting, Except for the edit appears Uitextfieldviewmodealways always appears} uitextfieldviewmode; //in the input box at the beginning of the text Text.text = @ " The text in the input box at the beginning "; //every character you enter into a word. Password input Text.securetextentry = yes; //Error correction Text.autocorrectiontype = uitextautocorrectiontypeno; typedef enum { Uitextautocorrectiontypedefault, default Uitextautocorrectiontypeno, no automatic error correction Uitextautocorrectiontypeyes, auto error correction} uitextautocorrectiontype; // Edit again to clear text.clearsonbeginediting = yes; //content alignment text.textalignment = Uitextalignmentleft; The vertical alignment of the //content uitextfield inherits from Uicontrol, which has an attribute in the class contentverticalalignment Text.contentverticalalignment = uicontrolcontentverticalalignmentcenter; // When set to YES, the text automatically shrinks to fit the text window size. The default is to keep the original size while letting long text scroll textfied.adjustsfontsizetofitwidth = yes; //Set the minimum font size for the display to automatically zoom out Text.minimumfontsize = 20; //Setting the keyboard style Text.keyboardtype = uikeyboardtypenumberpad; typedef enum { Uikeyboardtypedefault, default keyboard, all characters are supported Uikeyboardtypeasciicapable, default keyboard for ASCII uikeyboardtypenumbersandpunctuation, standard telephone keypad, support for +*# characters Uikeyboardtypeurl, url keyboards, support for. com buttons only support URL character Uikeyboardtypenumberpad, Digital keypad uikeyboardtypephonepad, phone keypad UIKEYBOARDTYPENAMEPHONEPAD, phone key Uikeyboardtypeemailaddress, keyboard uikeyboardtypedecimalpad for entering e-mail addresses, numeric keypad with digits and decimal nbsp Uikeyboardtypetwitter, optimized keypad for easy input @, #字符 Uikeyboardtypealphabet = uikeyboardtypeasciicapable, } uikeyboardtype; //Capital Letter Text.autocapitalizationtype = uitextautocapitalizationtypenone; typedef enum { Uitextautocapitalizationtypenone, not auto capital & nbsp; Uitextautocapitalizationtypewords, first letter capital Uitextautocapitalizationtypesentences, first letter of sentence Uitextautocapitalizationtypeallcharacters, all letters uppercase} Uitextautocapitalizationtype; //return key into what key text.returnkeytype =uireturnkeydone; typedef enum { Uireturnkeydefault, Default Gray button, marked with return Uireturnkeygo, blue button uireturnkeygoogle with go, Google blue button, search Uireturnkeyjoin, blue button labeled join Uireturnkeynext, blue button labeled next Uireturnkeyroute, blue button with 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 gray uikeyboardappearancealert, dark grey graphite } uireturnkeytype; //set Proxy Used to implement Protocol &NBsp Text.delegate = self; //Add TextField to view [Self.window addsubview:text]; //rightmost plus picture is similar to the left side of the code 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 receive becomeFirstResponder class to use uitextfielddelegate protocol text.delegate = self; Declare the text of the agent is me, I will go to the way the keyboard down the method in uitextfielddelegate so we have to use uitextfielddelegate this protocol -(BOOL) Textfieldshouldreturn: (Uitextfield *) textfield{ [text Resignfirstresponder]; //mainly [ Receiver Resignfirstresponder] Where to call the receiver corresponding to the keyboard to receive the return YES; Rewrite drawing behavior in addition to the style options of the Uitextfield object, you can also customize the Uitextfield object, adding many different rewrite methods to it 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: //change emoji properties. When overridden, calling super can be drawn by default drawing properties, if Once you have completely overridden the drawing function, you don't have to call super. –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 need 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://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{//Returns a BOOL value specifying whether the sequential text field starts editing return YES; -(void) textfielddidbeginediting: (Uitextfield *) textfield{ //Start editing, the Text field will become first responder} -(BOOL) textfieldshouldendediting: (Uitextfield *) textfield{//Returns a bool value that specifies whether the text field is allowed to end the edit, and when the edit is finished, the text field yields first responder//To prevent the text field from disappearing when the user finishes editing, you can return no// This is useful for programs where text fields must always remain active, 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//You can track the last modification made in a field, or you can log all edits for auditing purposes. //to prevent text from being changed you can return no//The parameter of this method has a Nsrange object, indicating the position of the changed text, the suggested text is also in which 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 removal of 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 will be picked up [TextField resignfirstresponder];//check 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 can also be used in a text field. In addition to the standard events of the Uicontrol class, you can also use the following Uitextfield class-specific events Uitextfieldtextdidbegineditingnotificationuitextfieldtextdidchangenotificationuitextfieldtextdidendeditingnotification when text triggered when the field exits edit mode. The object property of the notification stores the final text. Because text fields use the keyboard to enter text, actions are also sent when the following events occurNotification uikeyboardwillshownotification//keyboard display before sending uikeyboarddidshownotification // Keyboard is displayed after sending Uikeyboardwillhidenotification//keyboard hidden before sending uikeyboarddidhidenotification //keyboard hidden after sending 1, Text : Sets the default text for the text box. 2, Placeholder: Can be displayed in the text box gray words, to prompt the user should be in this text box to enter what content. When you enter data in this text box, the gray words for the hint will automatically disappear. 3, Background:4, Disabled: If this item is selected, the user will not be able to change the text box contents. 5. Next, there are three buttons 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 to the right of the small X, you can have the following options: 7.1 never appears: never appear &N Bsp 7.2 appears while editing: When editing occurs 7.3 appears unless editing: 7.4 is always visible: See 8, clear when editing begins: If this item is selected, the previous contents of the text box will be erased when you start editing the text box. For example, you now enter the text box a "what", then to edit the text box B, if you come back to edit the text box A, then the "what" will be immediately cleared. 9. Text color: Sets the colors of the text in the text box. 10. Font: Sets the font and font size of the text. 11, min Font Size: Sets the minimum font that the text box can display (but I don't feel like it) 12. Adjust to Fit: Specifies whether the text in the text box also shrinks when the text box size decreases. Select it to make all text visible, even if the text is long. However, this option is used with the Min font size, and the text shrinks, not less than the set min font size. The next section is used to set how the keyboardShown 13, Captitalization: Set uppercase. There are four options in the drop-down menu: 13.1 None: Caps 13.2 Words: Capitalize each word, the word here refers to a space-separated string 13.3 Sentanc ES: The first letter of each sentence is capitalized, and the sentence here is the string separated by a period plus a space 13.4 All characters: So the letter capital 14, Correction: Check spelling, default is yes. 15, Keyboard: Select the keyboard type, such as full numbers, letters and numbers. 16, Appearance:17, return key: Select the Return key, you can choose 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. Returns a BOOL value if the input box is Selected (checked) Enabled (available) highlighted (highlight) limit can 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:@ ""]; Separate groups by CS, array by @"" Separates the string BOOL canchange = [string isequaltostring:filtered]; return canchange;} Above that NUMBERS is a macro, can be defined at the top of the file: #define NUMBERS @ "0123456789n" (this representative can enter numbers and line breaks, please note that n, if you do not write this, the Done button will not trigger, If it is used in Searchbar, the search event will not be triggered because you are restricting the input to n, and I found it 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 make a hint before the above method return, such as prompting the user to enter only numbers. If you feel the need. Restrict the input of a certain length of character -(BOOL) TextField: (Uitextfield *) TextField Shouldchangecharactersinrange: (nsrange) Range Replacementstring: (NSString *) string; {//string is the character entered at this time TextField is the input box that is being entered returns Yes to change the value of the input box no opposite if ([string isequaltostring:@ "n"])//press ENTER 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] > 20) {//If the input box content is greater than 20 the warning pops up &N Bsp Textfield.text = [tobestring substringtoindex:20]; 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]; &NBS P return no; } } return yes; }
IOS Uitextfield Control Summary