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 edit appears uitextfieldviewmodealways always appear} uitextfieldviewmode; //set 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.
//the input box at the beginning of the text text.text = @ "The beginning of the text in the input box"; //each character into a point phrase password input Text.securetextentry = yes; //Error correction text.autocorrectiontype = uitextautocorrectiontypeno; typedef enum { uitextautocorrectiontypedefault, Default Uitextautocorrectiontypeno, no automatic error correction uitextautocorrectiontypeyes, automatic error correction} uitextautocorrectiontype; //Erase text.clearsonbeginediting = yes; //content Alignment text.textalignment = uitextalignmentleft; //The vertical alignment of the content uitextfield inherits from Uicontrol, There is 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 { &Nbsp; uikeyboardtypedefault, default keyboard, support all characters uikeyboardtypeasciicapable, default keyboard support for ASCII Uikeyboardtypenumbersandpunctuation, standard telephone keypad, supports +*# characters UIKeyboardTypeURL, url keyboard with support for. com buttons URL characters only uikeyboardtypenumberpad, Digital Keypad Uikeyboardtypephonepad, telephone keypad uikeyboardtypenamephonepad, telephone keypad, also support input names Uikeyboardtypeemailaddress, keyboards for entering electronic e-mail addresses Uikeyboardtypedecimalpad, numeric keypad with numbers and decimal points UIKeyboardTypeTwitter, optimized keypad for easy input @, #字符 uikeyboardtypealphabet = uikeyboardtypeasciicapable, } uikeyboardtype; Whether the first letter is capitalized text.autocapitalizationtype = uitextautocapitalizationtypenone; typedef enum { UITextAutocapitalizationTypeNone, do not automatically capitalize 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 button labeled Join uireturnkeynext, blue button with next uireturnkeyroute, blue button labeled route uireturnkeysearch, blue button with search uireturnkeysend, the 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 gray uikeyboardappearancealert, dark grey Graphite color } uireturnkeytype; //set proxy for implementing protocol text.delegate = self; // Add TextField to the view [self.window addsubview:text]; //the rightmost 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 keyboard down 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 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"  , 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 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; }
Uitextfield Full Solution