TextField set up detailed

Source: Internet
Author: User
Tags set background uicontrol

Initialize TextField and set location and size

  uitextfield *text = [[[Uitextfield Alloc]initwithframe:cgrectmake]]; //set border style, The border style is only displayed if it 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"]; //set background    text.disabledbackground = [ UIImage imagenamed:@ "Cc.png"];//when the input box does not have content, the WATERMARK hint   hint 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, heavy no show      UITextFieldViewModeWhileEditing,  when editing appears     uitextfieldviewmodeunlessediting, In addition to the edits appear     uitextfieldviewmodealways always appear} uitextfieldviewmode; //text in the input box from the beginning    text.text = @ "The beginning of the text in the input box";  //every character you enter into dots   phrases password input   text.securetextentry = yes; // Whether to correct   text.autocorrectiontype = uitextautocorrectiontypeno; typedef enum {     uitextautocorrectiontypedefault,  default     uitextautocorrectiontypeno, no automatic error correction      uitextautocorrectiontypeyes, auto-error correction} uitextautocorrectiontype; //clear    re-edit 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 = When uicontrolcontentverticalalignmentcenter; //is 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    Textfied.adjustsfontsizetofitwidth = yes; //Sets the minimum font size for the auto-shrink display   text.minimumfontsize = 20; // Set the keyboard style   text.keyboardtype = Uikeyboardtypenumberpad; typedef enum {     Uikeyboardtypedefault,      default keyboard, support all characters               uikeyboardtypeasciicapable, default keyboard support for ASCII      Uikeyboardtypenumbersandpunctuation, standard telephone keypad, supports +*# characters     UIKeyboardTypeURL,             url keyboard, support for. com buttons   Only URL character Uikeyboardtypenumberpad supported,              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; //If the initial letter is capitalized   text.autocapitalizationtype = Uitextautocapitalizationtypenone;  typedef enum {    uitextautocapitalizationtypenone,  not automatically capitalized      Uitextautocapitalizationtypewords, capitalize the first letter of the word     uitextautocapitalizationtypesentences, Capitalize 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, marked with Return    uireturnkeygo,      labeled Go Blue button     uireturnkeygoogle, with Google's blue button, term search      uireturnkeyjoin, blue with joinButton     uireturnkeynext, blue button labeled Next     uireturnkeyroute, blue button labeled route     uireturnkeysearch, blue button labeled Search     uireturnkeysend, blue button with send     uireturnkeyyahoo, blue button     uireturnkeyyahoo with Yahoo, 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   to implement protocol    Text.delegate = self; //Add 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 {&Nbsp;   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 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 the Uitextfield object's style options, 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.  – textrectforbounds:   //rewrite to reset text area – drawtextinrect:        //Change emoji properties. When overridden, calling super can be drawn by default drawing properties, and if you completely override the drawing function, you do not 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 have to call super if you completely override the drawing function .–  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 specifying whether to start editing in a sequential text field      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, and when the edit is finished, the text field is 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 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 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   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  //keyboard hidden after sending 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 now to the right of the small  x , you can have the following options:     7.1 never appears :  never appears     7.2 appears while editing :  edit appears     7.3 appears unless EDIting :     7.4 is always visible :  visible 8, Clear when editing begins :   If selected, the previous contents of the text box will be erased when you start editing the text box. 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 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  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 :  Capitalize each word, the word here refers to a space-separated string     13.3 sentances :  The first letter of each sentence, where the sentence is a string separated by a full 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 to 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:@ ""]; //groups separated by CS, array separated by @ ""      bool canchange = [string isequaltostring:filtered];      return canchange;}   The above NUMBERS is a macro that can be defined at the top of the file: #define NUMBERS @ "0123456789n"   (this delegate can enter a numberWord and newline, 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 have to restrict the input n, it is very sad, I found 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 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 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] > { //If the input box content is greater than 20 pop-up 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];              return NO;          }     }     return YES; }

TextField settings in detail

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.