Ios-textfield know how much

Source: Internet
Author: User
Tags uicontrol
<span id="Label3"></p><p><p>Initialize TextField and set location and size</p></p><p><p>Uitextfield *text = [[uitextfield alloc]initwithframe:cgrectmake (20, 20, 130, 30)];</p></p><p><p></p></p><p><p>Set border style to show border style only if set</p></p><p><p>Text.borderstyle = uitextborderstyleroundedrect;</p></p><p><p></p></p><p><p>typedef enum {</p></p><p><p>uitextborderstylenone,</p></p><p><p>uitextborderstyleline,</p></p><p><p>uitextborderstylebezel,</p></p><p><p>Uitextborderstyleroundedrect</p></p><p><p>} uitextborderstyle;</p></p><p><p></p></p><p><p>Sets the background color of the input box, which is set to white if you use a custom background picture border is ignored</p></p><p><p>Text.backgroundcolor = [uicolor whitecolor];</p></p><p><p></p></p><p><p>Set background</p></p><p><p>Text.background = [UIImage imagenamed:@ "dd.png"];</p></p><p><p></p></p><p><p>Set background</p></p><p><p>Text.disabledbackground = [UIImage imagenamed:@ "cc.png"];</p></p><p><p></p></p><p><p>When the input box does not have content, the watermark prompt content is password</p></p><p><p>Text.placeholder = @ "password";</p></p><p><p></p></p><p><p>Set font style and size for input box contents</p></p><p><p>Text.font = [uifont fontwithname:@ "Arial" size:20.0f];</p></p><p><p></p></p><p><p>Set Font Color</p></p><p><p>Text.textcolor = [uicolor redcolor];</p></p><p><p></p></p><p><p>Whether there is a fork in the input box, when it appears, to delete the contents of the input box at once</p></p><p><p>Text.clearbuttonmode = uitextfieldviewmodealways;</p></p><p><p></p></p><p><p>typedef enum {</p></p><p><p>uitextfieldviewmodenever, the heavy does not appear</p></p><p><p>uitextfieldviewmodewhileediting, appearing when editing</p></p><p><p>uitextfieldviewmodeunlessediting, except for the Editor's appearance.</p></p><p><p>Uitextfieldviewmodealways always Appears.</p></p><p><p>} uitextfieldviewmode;</p></p><p><p></p></p><p><p>Enter the text from the beginning of the box</p></p><p><p>Text.text = @ "the Beginning of the text in the input box";</p></p><p><p></p></p><p><p>Every character you enter becomes a point. password input</p></p><p><p>Text.securetextentry = YES;</p></p><p><p></p></p><p><p>Whether to correct</p></p><p><p>Text.autocorrectiontype = uitextautocorrectiontypeno;</p></p><p><p></p></p><p><p>typedef enum {</p></p><p><p>uitextautocorrectiontypedefault, Default</p></p><p><p>uitextautocorrectiontypeno, No automatic error correction</p></p><p><p>uitextautocorrectiontypeyes, Automatic error correction</p></p><p><p>} uitextautocorrectiontype;</p></p><p><p></p></p><p><p>Edit it again and empty it.</p></p><p><p>text.clearsonbeginediting = YES;</p></p><p><p></p></p><p><p>Content Alignment</p></p><p><p>Text.textalignment = uitextalignmentleft;</p></p><p><p></p></p><p><p>The vertical alignment of content Uitextfield inherits from uicontrol, which has an attribute in this class contentverticalalignment</p></p><p><p>Text.contentverticalalignment = uicontrolcontentverticalalignmentcenter;</p></p><p><p></p></p><p><p>When 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</p></p><p><p>Textfied.adjustsfontsizetofitwidth = YES;</p></p><p><p></p></p><p><p>Set the minimum font size for auto-shrink display</p></p><p><p>Text.minimumfontsize = 20;</p></p><p><p></p></p><p><p>Set the keyboard style</p></p><p><p>Text.keyboardtype = uikeyboardtypenumberpad;</p></p><p><p></p></p><p><p>typedef enum {</p></p><p><p>uikeyboardtypedefault, default keyboard, all characters supported</p></p><p><p>uikeyboardtypeasciicapable, default keyboard that supports ASCII</p></p><p><p>uikeyboardtypenumbersandpunctuation, Standard telephone keypad, supports +*# characters</p></p><p><p>uikeyboardtypeurl, URL keyboard, Support for. com buttons only support URL characters</p></p><p><p>uikeyboardtypenumberpad, Digital Keypad</p></p><p><p>uikeyboardtypephonepad, Telephone Keypad</p></p><p><p>uikeyboardtypenamephonepad, telephone keypad, also support input person name</p></p><p><p>uikeyboardtypeemailaddress, keyboard for entering e-mail addresses</p></p><p><p>uikeyboardtypedecimalpad, numeric keypad with numbers and decimal points</p></p><p><p>uikeyboardtypetwitter, optimized keyboard for easy input @, #字符</p></p><p><p>Uikeyboardtypealphabet = uikeyboardtypeasciicapable,</p></p><p><p>} uikeyboardtype;</p></p><p><p></p></p><p><p>Whether the first letter is capitalized</p></p><p><p>Text.autocapitalizationtype = uitextautocapitalizationtypenone;</p></p><p><p></p></p><p><p>typedef enum {</p></p><p><p>uitextautocapitalizationtypenone, not automatically capitalized</p></p><p><p>uitextautocapitalizationtypewords, capitalize the first letter of the word</p></p><p><p>uitextautocapitalizationtypesentences, capitalize the first letter of the sentence</p></p><p><p>uitextautocapitalizationtypeallcharacters, All letters are Capitalized.</p></p><p><p>} uitextautocapitalizationtype;</p></p><p><p></p></p><p><p>Return key becomes what key</p></p><p><p>Text.returnkeytype =uireturnkeydone;</p></p><p><p></p></p><p><p>typedef enum {</p></p><p><p>uireturnkeydefault, default gray button, marked with return</p></p><p><p>uireturnkeygo, the blue button labeled Go</p></p><p><p>uireturnkeygoogle, a blue button labeled google, Search by language</p></p><p><p>uireturnkeyjoin, blue button labeled Join</p></p><p><p>uireturnkeynext, the blue button labeled Next.</p></p><p><p>uireturnkeyroute, a blue button labeled route</p></p><p><p>uireturnkeysearch, the blue button labeled Search</p></p><p><p>uireturnkeysend, the blue button labeled Send</p></p><p><p>uireturnkeyyahoo, the blue button labeled Yahoo!</p></p><p><p>uireturnkeyyahoo, the blue button labeled Yahoo!</p></p><p><p>uireturnkeyemergencycall, Emergency call button</p></p><p><p>} uireturnkeytype;</p></p><p><p></p></p><p><p>Keyboard appearance</p></p><p><p>textview.keyboardappearance=uikeyboardappearancedefault;</p></p><p><p>typedef enum {</p></p><p><p>uikeyboardappearancedefault, Default appearance, light grey</p></p><p><p>uikeyboardappearancealert, dark Grey Graphite</p></p><p><p></p></p><p><p>} uireturnkeytype;</p></p><p><p></p></p><p><p></p></p><p><p>Setting up proxies for implementing protocols</p></p><p><p>Text.delegate = self;</p></p><p><p></p></p><p><p>Add the TextField to the view</p></p><p><p>[self.window addsubview:text];</p></p><p><p></p></p><p><p>The rightmost plus picture is similar to the left of the following code</p></p><p><p>Uiimageview *image=[[uiimageview alloc] initwithimage:[uiimage imagenamed:@ "right.png"];</p></p><p><p>text.rightview=image;</p></p><p><p>Text.rightviewmode = uitextfieldviewmodealways;</p></p><p><p></p></p><p><p>typedef enum {</p></p><p><p>uitextfieldviewmodenever,</p></p><p><p>uitextfieldviewmodewhileediting,</p></p><p><p>uitextfieldviewmodeunlessediting,</p></p><p><p>Uitextfieldviewmodealways</p></p><p><p>} uitextfieldviewmode;</p></p><p><p></p></p><p><p></p></p><p><p>Press RETURN key to receive Becomefirstresponder</p></p><p><p></p></p><p><p>class to adopt the Uitextfielddelegate protocol</p></p><p><p></p></p><p><p>Text.delegate = self; Declare the text of the agent is me, I will go to the way to get the keyboard down the method in uitextfielddelegate so we have to use uitextfielddelegate this protocol</p></p><p><p></p></p><p><p>-(BOOL) textfieldshouldreturn: (uitextfield *) TextField</p></p><p><p>{</p></p><p><p>[text resignfirstresponder]; The main is [receiver resignfirstresponder] where the call to the receiver corresponding to the keyboard down</p></p><p><p>Return YES;</p></p><p><p>}</p></p><p><p></p></p><p><p></p></p><p><p>overriding Paint behavior</p></p><p><p>In addition to the style options of the Uitextfield object, you can also customize the Uitextfield object and add 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.</p></p><p><p></p></p><p><p>–textrectforbounds://rewrite to reset text area</p></p><p><p>–drawtextinrect://change the emoji Property. when overridden, calling super can be drawn by default drawing properties, so you don't have to call super if you completely override the drawing Function.</p></p><p><p>–placeholderrectforbounds://override to reset placeholder area</p></p><p><p>–drawplaceholderinrect://override to change the draw placeholder Property. when overridden, calling super can be drawn by default drawing properties, so you don't have to call super if you completely override the drawing Function.</p></p><p><p>–borderrectforbounds://rewrite to reset the edge area</p></p><p><p>–editingrectforbounds://rewrite to reset the editing area</p></p><p><p>–clearbuttonrectforbounds://rewrite to reset the Clearbutton position, changing the size may cause the picture of the button to be distorted</p></p><p><p>–leftviewrectforbounds:</p></p><p><p>–rightviewrectforbounds:</p></p><p><p></p></p><p><p>Delegate method</p></p><p><p></p></p><p><p>-(BOOL) textfieldshouldbeginediting: (uitextfield *) textfield{</p></p><p><p></p></p><p><p>Returns a bool value that specifies whether the sequential text field begins editing</p></p><p><p></p></p><p><p>Return YES;</p></p><p><p>}</p></p><p><p></p></p><p><p>-(void) textfielddidbeginediting: (uitextfield *) textfield{</p></p><p><p></p></p><p><p>Triggered when editing is started, the text field becomes first responder</p></p><p><p>}</p></p><p><p></p></p><p><p>-(BOOL) textfieldshouldendediting: (uitextfield *) textfield{</p></p><p><p></p></p><p><p>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</p></p><p><p>To prevent the text field from disappearing when the user finishes editing, you can return no</p></p><p><p>This is useful for programs where text fields must always remain active, such as instant messaging</p></p><p><p></p></p><p><p>Return NO;</p></p><p><p>}</p></p><p><p></p></p><p><p>-(BOOL) textField: (uitextfield*) textField shouldchangecharactersinrange: (nsrange) range Replacementstring: ( NSString *) string{</p></p><p><p></p></p><p><p>This method is called when the user uses the AutoCorrect feature to modify the input text to the recommended Text.</p></p><p><p>This is especially useful for applications that want to join the Undo option</p></p><p><p>You can track the last modification made within a field, or you can log all edits for auditing purposes.</p></p><p><p>To prevent text from being changed, you can return no</p></p><p><p>There is a Nsrange object in the parameter of this method, which indicates the position of the changed text, and the suggested text is in it.</p></p><p><p></p></p><p><p>Return YES;</p></p><p><p>}</p></p><p><p></p></p><p><p>-(BOOL) textfieldshouldclear: (uitextfield *) textfield{</p></p><p><p></p></p><p><p>Returns a bool value indicating whether content is allowed to be purged based on user request</p></p><p><p>Can be set under certain conditions to allow purging of content</p></p><p><p></p></p><p><p>Return YES;</p></p><p><p>}</p></p><p><p></p></p><p><p>-(BOOL) textfieldshouldreturn: (uitextfield *) textfield{</p></p><p><p></p></p><p><p>Returns a bool value that indicates whether the edit is allowed to end when the ENTER key is pressed</p></p><p><p>If you allow the Resignfirstresponder method to be called, this causes the end of the edit, and the keyboard will be closed [textField resignfirstresponder];</p></p><p><p>Check out the meaning of the word resign to understand this method.</p></p><p><p></p></p><p><p>Return YES;</p></p><p><p>}</p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p>Notice</p></p><p><p>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</p></p><p><p></p></p><p><p>Uitextfieldtextdidbegineditingnotification</p></p><p><p>Uitextfieldtextdidchangenotification</p></p><p><p>Uitextfieldtextdidendeditingnotification</p></p><p><p>triggered when the text field exits edit Mode. The object property of the notification stores the final Text.</p></p><p><p></p></p><p><p>Because text fields use the keyboard to enter text, action notifications are also sent when the following events occur</p></p><p><p></p></p><p><p>Uikeyboardwillshownotification//keyboard Display before sending</p></p><p><p>Uikeyboarddidshownotification//keyboard Display after send</p></p><p><p>Uikeyboardwillhidenotification//keyboard Hidden before sending</p></p><p><p>Uikeyboarddidhidenotification//keyboard Hidden after sending</p></p><p><p></p></p><p><p>1. Text: Sets the default text for the text box.</p></p><p><p>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.</p></p><p><p>3, Background:</p></p><p><p>4, Disabled: If this item is selected, the user will not be able to change the text box Contents.</p></p><p><p>5. next, There are three buttons to set the Alignment.</p></p><p><p>6, Border style: Select the border style.</p></p><p><p>7, Clear Button: This is a drop-down menu, you can choose to clear when the button appears, the so-called Clear button is a text box to the right of the small X, you can have the following options:</p></p><p><p>7.1 Never appears: never appears</p></p><p><p>7.2 appears while editing: appears when editing</p></p><p><p>7.3 appears unless editing:</p></p><p><p>7.4 is always visible:</p></p><p><p>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.</p></p><p><p>9. Text Color: Sets the colors of the text in the text box.</p></p><p><p>10. Font: Sets the font and font size of the Text.</p></p><p><p>11, min Font Size: sets the minimum font that the text box can display (but I don't feel like it)</p></p><p><p>12. Adjust to Fit: Specifies whether the text in the text box also 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 is used with the Min font size, and the text shrinks, not less than the set min font size.</p></p><p><p>The next sections are used to set how the keyboard is Displayed.</p></p><p><p>13, Captitalization: Set Uppercase. There are four options in the Drop-down menu:</p></p><p><p>13.1 None: Uppercase Not set</p></p><p><p>13.2 Words: Capitalize the first letter of each word, where the word refers to a string separated by a space</p></p><p><p>13.3 Sentances: The first letter of each sentence is capitalized, and the sentence here is a string separated by a period plus a space</p></p><p><p>13.4 All characters: so uppercase letters</p></p><p><p>14, Correction: Check spelling, default is Yes.</p></p><p><p>15, Keyboard: Select the keyboard type, such as full numbers, letters and Numbers.</p></p><p><p>16, appearance:</p></p><p><p>17, return key: Select the return key, you can choose Search, return, done, and so On.</p></p><p><p>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.</p></p><p><p>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.</p></p><p><p></p></p><p><p>1.Alignment Horizontal Horizontal alignment</p></p><p><p>2.Alignment Vertical Vertical Alignment</p></p><p><p>3. Returns a bool value if the input box is Selected (checked) Enabled (available) highlighted (highlighted)</p></p><p><p></p></p><p><p>Restrict input of only specific characters</p></p><p><p></p></p><p><p>(BOOL) textField: (uitextfield *) textField shouldchangecharactersinrange: (nsrange) range replacementstring: (nsstring *) string{</p></p><p><p>Nscharacterset *cs;</p></p><p><p>CS = [[nscharacterset charactersetwithcharactersinstring:numbers]invertedset];</p></p><p><p></p></p><p><p>NSString *filtered = [[string componentsseparatedbycharactersinset:cs]componentsjoinedbystring:@ "]; Separate groups by cs, array separated by @ ""</p></p><p><p></p></p><p><p>BOOL Canchange = [string isequaltostring:filtered];</p></p><p><p></p></p><p><p>Return canchange;</p></p><p><p>}</p></p><p><p></p></p><p><p>The numbers above is a macro that can be defined at the top of the file:</p></p><p><p>#define NUMBERS @ "0123456789\n" (this representative can enter numbers and line breaks, Please note 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 restrict the input \ n, I found it in the Project. )</p></p><p><p>so, if you want to restrict the input of English and numbers, you can define this as:</p></p><p><p>#define KALPHANUM @ "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789″.</p></p><p><p>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.</p></p><p><p></p></p><p><p>Restrict the input of only a certain length of characters</p></p><p><p></p></p><p><p>-(BOOL) textField: (uitextfield *) textField shouldchangecharactersinrange: (nsrange) range Replacementstring: ( NSString *) string;</p></p><p><p>{//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</p></p><p><p></p></p><p><p>If ([string isequaltostring:@ "\ n"])//press passing to change</p></p><p><p>{</p></p><p><p>Return YES;</p></p><p><p>}</p></p><p><p></p></p><p><p>NSString * tobestring = [textfield.text Stringbyreplacingcharactersinrange:range withstring:string]; Get the contents of the input box</p></p><p><p></p></p><p><p>if (self.mytextfield = = TextField)//determine If we want to limit the input box</p></p><p><p>{</p></p><p><p>If ([tobestring length] > 20) {//popup Warning If input box content is greater than 20</p></p><p><p>Textfield.text = [tobestring substringtoindex:20];</p></p><p><p>Uialertview *alert = [[[uialertview alloc] initwithtitle:nil message:@ "the Maximum number of words cannot be entered" delegate:nil cancelbuttontitle:@ " OK "otherbuttontitles:nil, nil] autorelease];</p></p><p><p>[alert show];</p></p><p><p>Return NO;</p></p><p><p>}</p></p><p><p>}</p></p><p><p>Return YES;</p></p><p><p>}</p></p><p><p>This is reproduced, the original address: http://blog.csdn.net/tskyfree/article/details/8121915</p></p><p><p>Ios-textfield know how much</p></p></span>

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.