iOS Stage Learning 29th Day notes (uitextfield's Introduction)
Last Update:2015-07-30
Source: Internet
Author: User
<span id="Label3"></p><p style="text-align: center;"><p style="text-align: center;">iOS Learning (UI) Knowledge Point Grooming<br><br></p></p><p><p>first, about the introduction of Uitextfield<br><br>1) concept: Uitextfield is a control for receiving user input<br><br>2) Uitextfield Initialization Instance Code:</p></p><pre><span style="color: #008080;"><span style="color: #008080;">1</span></span> <span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Create a Uitextfield instance</span></span><span style="color: #008080;"><span style="color: #008080;">2</span></span>Uitextfield *textfield =<span style="color: #000000;"><span style="color: #000000;">[[uitextfield alloc] init];</span></span><span style="color: #008080;"><span style="color: #008080;">3</span></span>Textfield.frame = CGRectMake (<span style="color: #800080;"><span style="color: #800080;">Ten</span></span>,<span style="color: #800080;"><span style="color: #800080;"></span> +</span>, self.view.frame.size.width-<span style="color: #800080;"><span style="color: #800080;"></span> -</span>,<span style="color: #800080;"><span style="color: #800080;"></span> +</span><span style="color: #000000;"><span style="color: #000000;">);</span></span><span style="color: #008080;"><span style="color: #008080;">4</span></span>Textfield.backgroundcolor =<span style="color: #000000;"><span style="color: #000000;">[uicolor lightgraycolor];</span></span><span style="color: #008080;"><span style="color: #008080;">5</span></span> <span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">set the text in Textfiled</span></span><span style="color: #008080;"><span style="color: #008080;">6</span></span> <span style="color: #000000;"><span style="color: #000000;">Textfield.text = @ "user name";</span></span><span style="color: #008080;"><span style="color: #008080;">7</span></span> <span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">set the font for textfiled</span></span><span style="color: #008080;"><span style="color: #008080;">8</span></span>Textfield.font = [uifont systemfontofsize:<span style="color: #800080;"><span style="color: #800080;"></span> -</span><span style="color: #000000;"><span style="color: #000000;">];</span></span><span style="color: #008080;"><span style="color: #008080;">9</span></span> <span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">set the text color of textfiled</span></span><span style="color: #008080;"><span style="color: #008080;">Ten</span></span>Textfield.textcolor =<span style="color: #000000;"><span style="color: #000000;">[uicolor purplecolor];</span></span><span style="color: #008080;"><span style="color: #008080;"></span> one</span>[self.view addsubview:textfield];</pre><p><p><br>3) TextAlignment Set the alignment of the text for example:</p></p><pre><pre><span style="color: #008080;">1</span> <span style="color: #008000;">//</span> <span style="color: #008000;">set text left-justified for textfiled</span> <span style="color: #008080;">2</span> textfield.textalignment = nstextalignmentleft;</pre></pre><p><p></p></p><p><p>4) BorderStyle Set the text box style for example:</p></p><pre><pre> <span style="color: #008080;">1 </span> <span style="color: #008000;">//</span> <span style set textfiled to no border < span> <span style="color: #008080;">2 </span> textfield.borderstyle =<span style="color: # 000000; "> uitextborderstylenone; </span> <span style="color: #008080;">3 </span> <span style="color: #008000; >//</span> <span style=" color: #008000; "> BorderStyle has the following types of </span> <span style="color: #008080;">4 </span> <span style="color: #008000;">//</span> <span style="color: #008000;">1, uitextborderstylenone, </span> <span style="color: #008080; >5 </span> <span style=" color: #008000; ">//</span> Span style= "color: #008000;" >2, uitextborderstyleline, </span> <span style="color: #008080; >6 </span> <span style=" color: #008000; ">//</span> Span style= "color: #008000;" >3, uitextborderstylebezel, <span style="color: #008080; >7 </span> <span style=" color: #008000; ">//</span> <span style="color: #008000;" "4, uitextborderstyleroundedrect < span> </span></pre></pre><p><p><br>5) Layer.cornerradius Set the text box fillet for example:</p></p><pre><pre><span style="color: #008080;">1</span> <span style="color: #800080;">4.0f</span>;</pre></pre><p><p></p></p><p><p>6) layer.borderwidth Set the text box border width for example:</p></p><pre><pre><span style="color: #008080;">1</span> <span style="color: #800080;">1</span>;</pre></pre><p><p></p></p><p><p>7) layer.bordercolor set text box border color for example:</p></p><pre><pre><span style="color: #008080;">1</span> TextField.layer.borderColor = [uicolor darkgraycolor]. cgcolor;</pre></pre><p><p><br>8) background Set the text box background picture for example:</p></p><pre><pre><span style="color: #008080;">1</span> UIImage *image = [UIImage imagenamed:<span style="color: #800000;">@ "</span><span style="color: #800000;">btnemojbtn</span><span style="color: #800000;">"</span><span style="color: #000000;">];</span> <span style="color: #008080;">2</span> Textfield.background = image;</pre></pre><p><p><br>9) Set text box default display text (default gray font text disappears when you click Keyboard Input) for Example:<br>Method One: Placeholder</p></p><pre><pre><span style="color: #008080;">1</span> <span style="color: #800000;">@"</span> <span style="color: #800000;">User name</span> <span style="color: #800000;">"</span>;</pre></pre><p><p>Method Two: Nsmutableattributedstring</p></p><pre><pre><span style="color: #008080;">1</span> nsmutableattributedstring *muattstr = [[nsmutableattributedstring alloc] initwithstring:<span style="color: #800000;">@ "</span> <span style="color: #800000;">username</span> <span style="color: #800000;"> "</span><span style="color: #000000;">];</span> <span style="color: #008080;">2</span> <span style="color: #000000;">[muattstr addattribute:nsforegroundcolorattributename value:</span><span style="color: #008080;">3</span> [uicolor bluecolor] range: Nsmakerange (<span style="color: #800080;">0</span><span style="color: #000000;">, muattstr.length)];</span> <span style="color: #008080;">4</span> textfield.attributedplaceholder = muattstr;</pre></pre><p><p><br>Clearbuttonmode set the Clear Content button display mode for the text box (that is, The small fork to the right of the text box) for example:</p></p><pre><pre>Textfield.clearbuttonmode =<span style="color: #000000;"> uitextfieldviewmodewhileediting;<br></span><span style="color: #0000ff;">enum</span> <span style="color: #000000;">{ </span><span style="color: #008000;">//</span> <span style="color: #008000;">never appear</span> <span style="color: #008000;">//</span> uitextfieldviewmodeunlessediting <span style="color: #008000;">when editing</span> , <span style="color: #008000;">//</span> <span style="color: #008000;">appears</span> in addition to editing Uitextfieldviewmodealways <span style="color: #008000;">//</span> <span style="color: #008000;">always appears</span> } uitextfieldviewmode;</pre></pre><p><p><br>One) Leftview Set the left view of the text box for Example:</p></p><pre><pre><span style="color: #008080;">1</span> UIView *leftview =<span style="color: #000000;"> [[UIView alloc] init];</span> <span style="color: #008080;">2</span> Leftview.backgroundcolor =<span style="color: #000000;"> [uicolor clearcolor];</span> <span style="color: #008080;">3</span> Leftview.frame = CGRectMake (<span style="color: #800080;">0</span><span style="color: #800080;">0</span><span style="color: #800080;"></span><span style="color: #800080;"></span><span style="color: #000000;">);</span> <span style="color: #008080;">4</span> Textfield.leftview =<span style="color: #000000;"> iconimgview;</span> <span style="color: #008080;">5</span> <span style="color: #008000;">//</span> <span style="color: #008000;">set how the left view of the text box appears</span> <span style="color: #008080;">6</span> Textfield.leftviewmode = uitextfieldviewmodealways;</pre></pre><p><p></p></p><p><p>Returnkeytype Set the text box to exhale the lower right button type of the keyboard for example:</p></p><pre><pre><span style="color: #008080;">1</span> textfield.returnkeytype = uireturnkeydone;</pre></pre><p><p></p></p><p><p>Userinteractionenabled set whether the text box can be edited for example:</p></p><pre><pre><span style="color: #008080;">1</span> <span style="color: #008000;">//</span> <span style="color: #008000;">Setting a text box is not editable</span> <span style="color: #008080;">2</span> textfield.userinteractionenabled=no;</pre></pre><p><p></p></p><p><p>Becomefirstresponder Setting the text box to be the first responder is to exhale the keyboard for example:</p></p><pre><pre><span style="color: #008080;">1</span> <span style="color: #000000;">[textField becomefirstresponder];</span> <span style="color: #008080;">2</span> <span style="color: #008000;">//</span> <span style="color: #008000;">note: When you set a control to be the first responder, then set the other as the first responder invalid first responder has and can only have one</span></pre></pre><p><p></p></p><p><p>Resignfirstresponder Setting the text box to discard the first responder is to hide the keyboard for example:</p></p><pre><pre><span style="color: #008080;">1</span> [textField resignfirstresponder];</pre></pre><p><p></p></p><p><p>Securetextentry Set text box redaction mode that is password box for example:</p></p><pre><pre><span style="color: #008080;">1</span> TextField. securetextentry = YES;</pre></pre><p><p><br>Keyboardtype Setting the text box outbound keyboard type for example:</p></p><pre><span style="color: #008080;"><span style="color: #008080;">1</span></span> <span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">setting up the numeric keypad</span></span><span style="color: #008080;"><span style="color: #008080;">2</span></span>Textfield.keyboardtype =<span style="color: #000000;"><span style="color: #000000;">uikeyboardtypenumberpad;</span></span><span style="color: #008080;"><span style="color: #008080;">3</span></span> <span style="color: #008080;"><span style="color: #008080;">4</span></span> <span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Keyboardtype are:</span></span><span style="color: #008080;"><span style="color: #008080;">5</span></span>typedef<span style="color: #0000ff;"><span style="color: #0000ff;">enum</span></span><span style="color: #000000;"><span style="color: #000000;"> {</span></span><span style="color: #008080;"><span style="color: #008080;">6</span></span>uikeyboardtypedefault,<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">default keyboard, all characters supported</span></span><span style="color: #008080;"><span style="color: #008080;">7</span></span>uikeyboardtypeasciicapable,<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">default keyboard that supports ASCII</span></span><span style="color: #008080;"><span style="color: #008080;">8</span></span>uikeyboardtypenumbersandpunctuation,<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Standard telephone keypad with +*# character support</span></span><span style="color: #008080;"><span style="color: #008080;">9</span></span>uikeyboardtypeurl,<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">URL keyboard, Support for. com buttons only support URL characters</span></span><span style="color: #008080;"><span style="color: #008080;">Ten</span></span>uikeyboardtypenumberpad,<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Numeric Keypad</span></span><span style="color: #008080;"><span style="color: #008080;"></span> one</span>uikeyboardtypephonepad,<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Telephone keypad</span></span><span style="color: #008080;"><span style="color: #008080;"></span> a</span>uikeyboardtypenamephonepad,<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">telephone keypad, also support input person name</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span>uikeyboardtypeemailaddress,<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">keyboard for entering e-mail addresses</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span>uikeyboardtypedecimalpad,<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">numeric keypad with numbers and decimal</span> points</span><span style="color: #008080;"><span style="color: #008080;"></span> the</span>uikeyboardtypetwitter,<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">optimized keyboard for easy input @, #字符</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span>Uikeyboardtypealphabet =<span style="color: #000000;"><span style="color: #000000;">uikeyboardtypeasciicapable,</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span>} uikeyboardtype;</pre><p><p></p></p><p><p>Adjustsfontsizetofitwidth when you set the text box content to be outside the text box range, The text automatically changes size, for example:</p></p><pre><pre><span style="color: #008080;">1</span> textfield.adjustsfontsizetofitwidth =<span style="color: #000000;"> YES;</span> <span style="color: #008080;">2</span> <span style="color: #008000;">//</span> <span style="color: #008000;">set the minimum font size for auto-shrink display</span> <span style="color: #008080;">3</span> <span style="color: #800080;"></span>;</pre></pre><p><p></p></p><p><p>Keyboardappearance Set the keyboard look for example:</p></p><pre><pre><span style="color: #008000;">//</span> <span style="color: #008000;">dark grey Graphite color keyboard</span> <span style="color: #800080;">2</span> textfield.keyboardappearance=<span style="color: #000000;">uikeyboardappearancealert;</span> <span style="color: #800080;">3</span> <span style="color: #0000ff;">enum</span> <span style="color: #000000;">{</span><span style="color: #800080;">4</span><span style="color: #008000;">//</span> <span style="color: #008000;">default appearance, light gray</span> <span style="color: #800080;">5</span> uikeyboardappearancealert, <span style="color: #008000;">//</span> <span style="color: #008000;"></span> <span style="color: #800080;">6</span> } uireturnkeytype;</pre></pre><p><p></p></p><p><p>20) text box content alignment for Example:</p></p><pre><pre><span style="color: #008080;">1</span> <span style="color: #008000;">//</span> Vertical alignment of <span style="color: #008000;"> content Uitextfield inherits from uicontrol, which has an attribute contentverticalalignment</span><span style="color: #008080;">2</span> textfield.contentverticalalignment = uicontrolcontentverticalalignmentcenter;</pre></pre><p><p></p></p><p><p>) font set font style and size for text boxes such as:</p></p><pre><pre><span style="color: #008080;">1</span> Textfield.font = [uifont fontwithname:<span style="color: #800000;">@ "</span><span style="color: #800000;">Arial</span><span style="color: #800000;">"</span> size:<span style="color: #800080;">20.0f</span>];</pre></pre><p><p><br>Delegate Set text box proxy object for Example:</p></p><pre>TextField.<span style="color: #0000ff;"><span style="color: #0000ff;">Delegate</span></span>=<span style="color: #000000;">self <span style="color: #000000;">;</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">When the text box is set to proxy, the current class must first comply with the Uitextfielddelegate protocol and then implement its Protocol method</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">its protocol methods are:</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">returns a bool value whether the text box is allowed to start editing</span></span> <span style="color: #800080;"><span style="color: #800080;">1</span></span>,-(BOOL) textfieldshouldbeginediting: (uitextfield *<span style="color: #000000;"><span style="color: #000000;">) textField;</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">triggered when editing starts, the text box becomes the first responder (responder)</span></span><span style="color: #800080;"><span style="color: #800080;">2</span></span>、- (<span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span>) textfielddidbeginediting: (uitextfield *<span style="color: #000000;"><span style="color: #000000;">) textField;</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">returns the bool value, whether the text box is allowed to end editing, and when the edit is finished, the current text box discards the first responder identity</span></span><span style="color: #800080;"><span style="color: #800080;">3</span></span>,-(BOOL) textfieldshouldendediting: (uitextfield *<span style="color: #000000;"><span style="color: #000000;">) textField;</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">This method is triggered when the text box finishes editing</span></span><span style="color: #800080;"><span style="color: #800080;">4</span></span>、- (<span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span>) textfielddidendediting: (uitextfield *<span style="color: #000000;"><span style="color: #000000;">) textField;</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">This method is triggered when text content changes, and can be used to filter or restrict the text content in this method</span></span><span style="color: #800080;"><span style="color: #800080;">5</span></span>,-(BOOL) textField: (uitextfield *<span style="color: #000000;"><span style="color: #000000;">) textfieldshouldchangecharactersinrange: (nsrange) range replacementstring: (nsstring</span></span>*)<span style="color: #0000ff;"><span style="color: #0000ff;">string</span></span><span style="color: #000000;"><span style="color: #000000;">; </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">This method is triggered when you click the Clear button on the right of the text box</span></span><span style="color: #800080;"><span style="color: #800080;">6</span></span>,-(BOOL) textfieldshouldclear: (uitextfield *<span style="color: #000000;"><span style="color: #000000;">) textField; </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">This method is triggered when you click the return button in the lower right corner of the keyboard</span></span><span style="color: #800080;"><span style="color: #800080;">7</span></span>,-(BOOL) textfieldshouldreturn: (uitextfield *) textField;</pre><p><p></p></p><p><p>iOS Stage Learning 29th Day notes (uitextfield's Introduction)</p></p></span>