ios-uitextfield-Total Solution

Source: Internet
Author: User

Initialize TextField and set location and size

  uitextfield *text = [[Uitextfield Alloc]initwithframe:cgrectmake]];   // The border style is set only if the border style is set    text.borderstyle = uitextborderstyleroundedrect;  typedef enum {      UITextBorderStyleNone,      UITextBorderStyleLine,      uitextborderstylebezel,     uitextborderstyleroundedrect      } uitextborderstyle;   //sets the background color of the input box, which is set to white   If you use a custom background picture border is ignored   Text.backgroundcolor = [Uicolor whitecolor];   //setting background    text.background = [UIImage imagenamed:@ "Dd.png"];   //setting background     text.disabledbackground = [UIImage imagenamed:@ "Cc.png"]; //when the input box does not have content, the watermark prompt   prompt content is Password   text.placeholder = @ "password";    //set 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];   //the input box has a fork number, when it is displayed, Used to delete the contents of the input box once    text.clearbuttonmode = Uitextfieldviewmodealways;   typedef enum {      uitextfieldviewmodenever, heavy no show       uitextfieldviewmodewhileediting,       uitextfieldviewmodeunlessediting appears when editing, In addition to the editor appears      uitextfieldviewmodealways has been appearing  } uitextfieldviewmode;    //in the input box at the beginning of the text    text.text = @ "At the beginning of the text in the input box;    //every character you enter into dots   phrases password input    text.securetextentry = yes;   ios-uitextfield-change cursor color

Method 1:

[[Uitextfield appearance] Settintcolor:[uicolor Blackcolor]];

This approach will affect all TextField.

Method 2:

Textfield.tintcolor = [Uicolor Redcolor];

If you modify the view's Tintcolor property in Interfacebuilder, it is not useful.

 //whether error correction    text.autocorrectiontype = uitextautocorrectiontypeno;   typedef enum {     uitextautocorrectiontypedefault,  default       Uitextautocorrectiontypeno, no automatic error correction      uitextautocorrectiontypeyes, automatic error correction  } uitextautocorrectiontype;   //re-edit to empty    text.clearsonbeginediting = YES;     //content Alignment    text.textalignment = uitextalignmentleft;   // The vertical alignment of content   uitextfield inherits from Uicontrol, which has an attribute in this 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 auto-zoom    text.minimumfontsize = 20;   //Set keyboard style     Text.keyboardtype = uikeyboardtypenumberpad;   typedef enum {     uikeyboardtypedefault,      default keyboard, Supports all characters                Uikeyboardtypeasciicapable, the default keyboard      uikeyboardtypenumbersandpunctuation that supports ASCII, Standard telephone keypad with support for +*# characters      UIKeyboardTypeURL,             url keyboard, support for. com buttons   only URL characters are supported  UIKeyboardTypeNumberPad,              Numeric keypad  uikeyboardtypephonepad, telephone keypad       uikeyboardtypenamephonepad, telephone keypad, also supports input person name  uikeyboardtypeemailaddress, keyboard for entering electronic   e-mail address        uikeyboardtypedecimalpad,    numeric keypad   with numbers and decimal points       uikeyboardtypetwitter,       optimized keypad for easy input @, #字符       Uikeyboardtypealphabet = Uikeyboardtypeasciicapable,  } uikeyboardtype;   //Capital Letter     Text.autocapitalizationtype = uitextautocapitalizationtypenone;   typedef enum {      UITextAutocapitalizationTypeNone,  do not automatically capitalize       Uitextautocapitalizationtypewords, capitalize the first letter of the word      uitextautocapitalizationtypesentences, The first letter of the sentence      UITextAutocapitalizationTypeAllCharacters,  all the letters capitalized  } Uitextautocapitalizationtype;   //return key into what key   text.returnkeytype =UIReturnKeyDone;    typedef enum {     uireturnkeydefault,  default   Gray button, marked with return      UIReturnKeyGo,     blue button with Go       Uireturnkeygoogle, blue button labeled Google, Search      uireturnkeyjoin, blue button labeled Join       uireturnkeynext, the blue button labeled Next     &NBsp Uireturnkeyroute, blue button labeled route      uireturnkeysearch, blue button with 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;   //Add 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;   &nbsP; text.rightviewmode = uitextfieldviewmodealways;    typedef enum {      UITextFieldViewModeNever,     UITextFieldViewModeWhileEditing,      UITextFieldViewModeUnlessEditing,      uitextfieldviewmodealways  uitextfieldviewmode;     //Press RETURN key to receive    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];    //is mainly [receiver Resignfirstresponder] where the call will be able to receive receiver corresponding 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:        //Change emoji properties. 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. If you completely rewrite the drawing function, you don't have to call super.  – borderrectforbounds://Override to reset the Edge area  – editingrectforbounds:// Rewrite to reset the editing area  – clearbuttonrectforbounds://Rewrite to reset the Clearbutton position, changing the size may cause the picture of the button to distort  –  leftviewrectforbounds: – rightviewrectforbounds:    Delegate Method    -  (BOOL ) Textfieldshouldbeginediting: (uitextfield *) textfield{   //Returns a bool value that specifies whether the sequential text field begins editing         return YES;  }    -  (void) TextfielDdidbeginediting: (uitextfield *) textfield{  //start editing, text field will become First responder  }     -  (BOOL) textfieldshouldendediting: (uitextfield *) textfield{ //Returns a BOOL value, Specifies whether to allow the text field to end editing, and 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 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  //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 purging of content       return YES;  }    -(BOOL) Textfieldshouldreturn: (uitextfield *) textfield{   // Returns a bool value that indicates whether to allow the edit to end when the ENTER key is pressed   //if the resignfirstresponder  method is allowed to be called, this causes the end of the edit, and the keyboard is closed [textfield  resignfirstresponder]; //check resign The meaning of the word 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 hidden after sending  1, text : Setting the default text boxThis.  2, placeholder :  can display gray words in a 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 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&nbsp, you can have the following options:      7.1 never appears :  never appears      7.2 appears while editing  :  when editing appears      7.3 appears unless editing :       7.4 is always visible :  visible  8, Clear when editing begins :  if this is selected, when you start editing this text box, The previous content in the text box is erased. For example, you are now in this text box  A  entered   "What" &nbsp, and then to edit the text box  b, if you come back to edit the text box  a&nbsp, 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 that when the size of the text box is reduced, the text boxText should also be scaled down. 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 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  :  the first letter of each word capitalized, the word here refers to a space separated by the string      13.3 sentances :  each sentence the first letter uppercase, The sentence here is the string separated by a period and a space      13.4 all characters :  so the letter capitalized  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 enter only 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 @ "" to isolate the string         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 representative can enter numbers and line breaks, 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 do not allow the input n, very miserable, 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 alsoBefore the FA return, do a hint, such as prompting the user to enter only numbers. If you feel the need.     restrict the input of characters    -(BOOL) TextField: (Uitextfield *) to a certain length TextField Shouldchangecharactersinrange: (nsrange) Range replacementstring: (NSString *) string;  {// String is the character entered at this time  textfield is the input box that is being entered at this point   return yes is the value of the input box can be changed  no opposite     if ([string isequaltostring:@ "n"])//press passing to change      {           return yes;      }         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] >) { // Pop-up warning if input box content is greater than 20  

Textfield.text = [tobestring substringtoindex:20];

Uialertview *alert = [[[Uialertview alloc] Initwithtitle:nil message:@ "The maximum number of words cannot be entered" Delegate:nil cancelbuttontitle:@ "              OK "otherbuttontitles:nil, nil] autorelease];              [Alert show];          return NO;  }} return YES; }

ios-uitextfield-Total Solution

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.