<span id="Label3"></p><p><p>Initialize TextField and set location and size</p></p>Uitextfield *text = [[uitextfield alloc]initwithframe:cgrectmake (20, 20, 130, 30)]; The border style is set only if the border style is set text.borderstyle = uitextborderstyleroundedrect; typedef enum {uitextborderstylenone, uitextborderstyleline, uitextborderstylebezel, uitextborderstylerounded Rect} uitextborderstyle; Sets the background color of the input box, when set to white if you use a custom background picture<span id="6_nwp">the <span id="6_nwp">border will be ignored</span></span>Text.backgroundcolor = [uicolor whitecolor]; Set Background Text.background = [UIImage imagenamed:@ "dd.png"]; Set the background Text.disabledbackground = [UIImage imagenamed:@ "cc.png"];//when The input box does not have content, the watermark prompt content is password Text.placeholder = @ "pa ssWOrd "; 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<span id="5_nwp"><span id="5_nwp">redcolor];</span></span> //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 does not appear uitextfieldviewmodewhileediting, when editing occurs uitextfieldviewmodeunlessediting, except edit appear Uitextfieldviewmodealways always appears} uitextfieldviewmode;& Nbsp;//input box in the beginning of the text Text.text = @ "the Beginning of the text in the input box," //each input a character becomes point password input Text.securetextentry = Yes; //error correction Text.autocorrectiontype = uitextautocorrectiontypeno; typedef enum { uitextautocorrectiontypedefault, default uitextautocorrectiontypeno, no automatic error correction uitextautocorrectiontypeyes, Auto Error correction} uitextautocorrectiontype; //again edit to empty text.clearsonbeginediting = YES; //content alignment Text.textalignment = vertical alignment of uitextalignmentleft; //content Uitextfield inherits from uicontrol, which has an attribute contentverticalalignment text.contentverticalalignment = UicontrolcontentverticalalignmeWhen ntcenter; //is 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 auto Zoom out Text.minimumfontsize = 20; //set the keyboard style Text.keyboardtype = uikeyboardtypenumberpad; typedef enum { uikeyboardtypedefault, default keyboard, all characters supported &NBSP;&NBSP ; uikeyboardtypeasciicapable, ascii-enabled default keyboard uikeyboardtypenumbersandpunctuation, standard telephone keypad, support for +*# characters uikeyboardtypeurl, URL keypad, support for. com buttons Only URL characters are supported uikeyboardtypenumberpad, numeric keypad uikeyboardtypephonepad, telephone keypad &NBSP ; uikeyboardtypenamephonepad, telephone keypad, also support input person name uikeyboardtypeemailaddress, for input electronic <span id="4_nwp"><span id="4_nwp">the keyboard for the email address</span></span>uikeyboardtypedecimalpad,<span id="3_nwp"><span id="3_nwp">numeric keypad with numbers and decimal</span> points</span> uikeyboardtypetwitter, optimized keypad for easy input @, #字符 Uikeyboardtypealphabet = uikeyboardtypeasciicapable, } uikeyboardtype; //capital Letter Text.autocapitalizationtype = uitextautocapitalizationtypenone; typedef enum { uitextautocapitalizationtypenone, not automatically capitalized uitextautocapitalizationtypewords, capitalize the first letter of the word uitextautocapitalizationtypesentences, The first letter of the sentence uitextautocapitalizationtypeallcharacters, all letters in uppercase} uitextautocapitalizationtype; //return key into what key text.returnkeytype =uireturnkeydone; typedef enum { UIReturnKeyDefault, Default Gray button, labeled return uireturnkeygo, blue button Uireturnkeygoogle for Go Blue button labeled google, search uireturnkeyjoin, blue button labeled join uireturnkeynext, blue button with next uireturnkeyroute, blue button labeled route uireturnkeysearch, blue button with search uireturnkeysend, The blue button labeled Send &NBSp uireturnkeyyahoo, blue Button labeled Yahoo! uireturnkeyyahoo, blue button with Yahoo! uireturnkeyemergencycall, emergency call button} Uireturnkeytype; //keyboard appearance textview.keyboardappearance= uikeyboardappearancedefault;typedef enum {uikeyboardappearancedefault, default appearance, light gray uikeyboardappearancealert, dark grey Graphite } uireturnkeytype; //setting Proxy for implementing protocol Text.delegate = self; //adding TextField to the view [self.window addsubview:text]; //the rightmost plus picture is the following code left like 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 the 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<p><p>Return YES;</p></p>In addition to the style options of the Uitextfield object, you can also customize the Uitextfield object, adding a number of different rewrite methods 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 the text area –drawtextinrect://change the emoji 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. –pl Aceholderrectforbounds://override to reset placeholder area –drawplaceholderinrect://override To change the draw placeholder Property. when overridden, calling super can press the default<span id="2_nwp"><span id="2_nwp">Graph Property drawing, If you completely rewrite the drawing function, you do not have to call Super.</span></span>– borderrectforbounds://override to reset the edge area – editingrectforbounds://override to reset the editing area – clearbuttonrectforbounds:/ /override to reset the Clearbutton position, changing the size of the button can cause the picture to be distorted – leftviewrectforbounds:– rightviewrectforbounds: delegate method - (BOOL) textfieldshouldbeginediting: (uitextfield *) textfield{//returns A BOOL value that specifies whether the sequential text field begins editing & nbsp return yes; } - (void) textfielddidbeginediting: (uitextfield *) textfield{ //starts Editing and the text field becomes first responder} - (BOOL) textfieldshouldendediting: (uitextfield *) textfield{//returns BOOL value, Specifies whether the text field is allowed to end the edit, When the edit is finished, the text field will yield First responder //to prevent the 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//you can track the last modification made in a field, or you can log all edits for auditing purposes. //to prevent the text from being changed 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 it return yes; } - (BOOL) textfieldshouldclear: (uitextfield *) textfield{ //returns A BOOL value indicating whether the content is allowed 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 will be 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 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, 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&nbsP After the keyboard is hidden, send 1, text : sets the default text for the text box. 2. placeholder : can display gray words in the text box to indicate what the user should enter in the text box. 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 contents of the text box. 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 appears 7.2 appears while editing : editing occurs 7.3 appears unless E diting : 7.4 are always visible : visible 8, Clear When editing begins : if this item is selected, When you start editing the text box, the previous content in the text box is Erased. 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 like It) 12, Adjust to fit : Specifies whether the text in the text box shrinks when the size of the text box Decreases. 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 minFont size . The next sections are used to 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 : Each word is capitalized, the word here refers to a string separated by a space 13.3 sentances : The first letter of each sentence, where the sentence is the string separated by a period and a space 13.4 all characters : So letter capital 14, correction : Check spelling, default is YES . 15, keyboard : Select keyboard type, 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 this option is 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 only specific characters can be entered (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 @ "" to isolate the string BOOL canchange = [string Isequaltostring:filte<span id="1_nwp"><span id="1_nwp">red];</span></span>Return canchange;} The above NUMBERS is a macro, can be defined at the top of the file: #define NUMBERS @ "0123456789n" (this representative can enter numbers and newline, Please note that this n, if you do not write this, done<span id="0_nwp">the <span id="0_nwp">key will not be triggered, and if 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. )</span></span>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 being entered at this time Return Yes to change the value of the input box no the opposite if ([string isequaltostring:@ "n"])//press passing to change { return yes; } NSString * tobestring = [textfield.text stringbyreplacin Gcharactersinrange: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 Out warning 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]; &N Bsp Return no; } } return yes; }<p><p> Full parsing of Uitextfield use in iOS </p> </p></span>
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service