IOS-UITextField-full solution, IOS-UITextField-

Source: Internet
Author: User
Tags uicontrol

IOS-UITextField-full solution, IOS-UITextField-

// Initialize textfield and set the position and size

UITextField * text = [[UITextField alloc] initWithFrame: CGRectMake (20, 20,130, 30)]; // set the border style. The border style text is displayed only when it is set. borderStyle = custom; typedef enum {UITextBorderStyleNone, UITextBorderStyleLine, UITextBorderStyleBezel, UITextBorderStyleRoundedRect} UITextBorderStyle; // set the background color of the input box, if the custom background image border is used, text is ignored. backgroundColor = [UIColor whiteColor]; // sets the background text. background = [UIImage imageNamed: @ "dd.png"]; // set the background text. disabledBackground = [UIImage imageNamed: @ "cc.png"]; // when there is no content in the input box, the watermark prompts that the content is password text. placeholder = @ "password"; // set the font style and size of the text in the input box. font = [UIFont fontWithName: @ "Arial" size: 20366f]; // set the font color to text. textColor = [UIColor redColor]; // whether there is a cross in the input box. When is it displayed? It is used to delete text in the input box at a time. clearButtonMode = UITextFieldViewModeAlways; typedef enum {resume, which does not repeat progress. During editing, expires appears. UITextFieldViewModeAlways appears all the time except for editing.} UITextFieldViewMode; // The text that comes in. text = @ "text in the input box at the beginning"; // Enter text in the text box every time a character is entered. secureTextEntry = YES; // whether to correct text. autocorrectionType = optional; typedef enum {UITextAutocorrectionTypeDefault, default UITextAutocorrectionTypeNo, no automatic error correction UITextAutocorrectionTypeYes, automatic error correction} UITextAutocorrectionType; // clear text after editing again. clearsOnBeginEditing = YES; // content alignment text. textAlignment = UITextAlignmentLeft; // The vertical alignment of the content. UITextField inherits from UIControl, and this class has a contentverticalignment text attribute. contentverticalignment = UIControlContentVerticalAlignmentCenter; // when set to YES, the text will be automatically reduced to adapt to the text window size. by default, the original size is kept, and the long text is rolled textFied. adjustsFontSizeToFitWidth = YES; // you can specify the minimum font size of text to be automatically reduced. minimumFontSize = 20; // set the keyboard style text. keyboardType = UIKeyboardTypeNumberPad; typedef enum {UIKeyboardTypeDefault, default keyboard, all characters supported, ASCII default keyboard, standard phone keyboard, + * # character UIKeyboardTypeURL, URL keyboard, supported. the com button only supports the URL character UIKeyboardTypeNumberPad, The UIKeyboardTypePhonePad, the telephone keyboard, and the telephone keyboard. You can also enter the name UIKeyboardTypeEmailAddress, the numeric keypad has numbers and decimal point UIKeyboardTypeTwitter. The optimized keypad facilitates the input of @ and # characters UIKeyboardTypeAlphabet = UIKeyboardTypeASCIICapable,} UIKeyboardType; // whether the first letter is capital text. autocapitalizationType = custom; typedef enum {UITextAutocapitalizationTypeNone. UITextAutocapitalizationTypeWords are not automatically capitalized, the first letter of a sentence is capitalized, and all letters are capitalized.} UITextAutocapitalizationType; // What key does the return key become text. returnKeyType = UIReturnKeyDone; typedef enum {UIReturnKeyDefault, UIReturnKeyNext, UIReturnKeyNext, UIReturnKeyRoute, UIReturnKeySearch, UIReturnKeySend, UIReturnKeySend, and UIReturnKeyYahoo, UIReturnKeyYahoo, UIReturnKeyEmergencyCall, and emergency call.} UIReturnKeyType; // specifies the keyboard appearance textView. keyboardAppearance = UIKeyboardAppearanceDefault; typedef enum {UIKeyboardAppearanceDefault, default appearance, light gray UIKeyboardAppearanceAlert, dark gray graphite color} UIReturnKeyType; // sets the proxy for implementing protocol text. delegate = self; // Add textfield to the view [self. window addSubview: text]; // Add an image on the rightmost side of the following code: UIImageView * image = [[UIImageView alloc] initWithImage: [UIImage imageNamed: @ "right.png"]; text. rightView = image; text. rightViewMode = plaintext; typedef enum {plaintext, UITextFieldViewModeAlways} UITextFieldViewMode; // press the return key to access the becomeFirstResponder class using the UITextFieldDelegate protocol text. delegate = self; declare that the proxy of text is me. I will implement the method of receiving the keyboard down in UITextFieldDelegate. So we need to use the UITextFieldDelegate protocol-(BOOL) textFieldShouldReturn :( UITextField *) textField {[text resignFirstResponder]; // It mainly refers to [Cycler resignFirstResponder], where can the keyboard corresponding to the Cycler be collected down?

Return YES;

} In addition to the style option of the UITextField object, you can also customize the UITextField object and add many different rewriting methods for it to change the display behavior of text fields. These methods return a CGRect structure and define the boundary range of each part in the text field. You can override the following methods. -TextRectForBounds: // rewrite to reset the text area-drawTextInRect: // modify the painted text attributes. during rewriting, you can call super to draw based on the default graphic attribute. If you completely rewrite the rendering function, you do not need to call super. -placeholderRectForBounds: // override to reset the placeholder area-drawPlaceholderInRect: // override to change the drawing placeholder attribute. during rewriting, you can call super to draw based on the default graphic attribute. If you completely rewrite the rendering function, you do not need to call super. -borderRectForBounds: // rewrite to reset the Edge Area-editingRectForBounds: // rewrite to reset the editing area-clearButtonRectForBounds: // rewrite to reset the clearButton position, changing the size may cause image distortion of the button-leftViewRectForBounds:-righ TViewRectForBounds: Delegate method-(BOOL) textFieldShouldBeginEditing :( UITextField *) textField {// return a BOOL value and specify whether to start editing the return YES;}-(void) textFieldDidBeginEditing :( UITextField *) triggered when textField {// starts editing. The text field will become first responder}-(BOOL) textFieldShouldEndEditing :( UITextField *) textField {// return BOOL value, specifies whether to allow end-editing of text fields. When the end of editing, the text field will give way to the first responder // to prevent text fields from disappearing when the end of editing, you can return NO //, which is useful for programs whose text fields must always be active, such as instant message re. Turn NO;}-(BOOL) textField :( UITextField *) textField shouldChangeCharactersInRange :( nsange) range replacementString :( NSString *) string {// when the user uses the automatic correction function, this method is called when the input text is modified to the recommended text. // This is especially useful for applications that want to add revocation options. // you can trace the last modification made in the field, or log all edits for auditing purposes. // To prevent text changes, NO can be returned. // The parameter of this method has a nsange object, indicating the location of the changed text. The recommended text also returns YES ;} -(BOOL) textFieldShouldClear :( UITextField *) textField {// return a BOOL value to indicate whether to allow content to be cleared based on user requests. // you can set the condition to allow content to be cleared. return YES;}-(BOOL) textFieldShouldReturn :( UITextField *) textField {// return a BOOL value indicating whether to allow end editing when the Enter key is pressed // If the resignFirstResponder method is allowed, this will lead to end editing, the keyboard will be collapsed [textField resignFirstResponder]; // check the meaning of the resign to understand this method. return YES;} The notification UITextField is derived from UIControl, so the notification system in the UIControl class can also be used in text fields. In addition to standard events of the UIControl class, you can also use the following UITextField-specific event uitextfieldtextdidbegineditingicationicationuitextfieldtextdidchangenotificationuitextfieldtextdidendeditingnotification triggered when the text field exits the editing. The object Property of the notification stores the final text. Because text fields use the keyboard to input text, when the following events occur, UIKeyboardWillShowNotification // send UIKeyboardDidShowNotification before the keyboard display // send uikeyboardwillenoenotification after UIKeyboardDidHideNotification is hidden // send uikeyboardidenotification before hidden keyboard: 1. Text: set the default text in the text box. 2. Placeholder: displays the gray text box, prompting you what to enter in this text box. When data is entered in this text box, the gray words used for the prompt will automatically disappear. 3. Background: 4. Disabled: If this option is selected, you cannot change the content of the text box. 5. Next, there are three buttons for setting alignment. 6. Border Style: select the Border Style. 7. Clear Button: this is a drop-down menu. You can select when the Clear Button appears. The Clear Button is a small X on the right of the text box. You can select the following options: 7.1 Never appears: Never show 7.2 Appears while editing: 7.3 Appears unless editing: 7.4 Is always visible: always visible 8. Clear when editing begins: If this option Is selected, when you start to edit the text box, the previous content in the text box will be cleared. For example, if you enter "What" in text box A, then edit text box B. If you come back to edit text box A, "What" is immediately cleared. 9. Text Color: Set the Text Color in the Text box. 10. Font: Set the Font and Font size of the text. 11. Min Font Size: Set the smallest Font that can be displayed in the text box (but I don't think it is useful) 12. Adjust To Fit: specifies that when the text box Size is reduced by an hour, whether the text in the text box must be reduced. Select it to make all the text visible, even if the text is too long. However, this option should be used with Min Font Size, and the text will be reduced, and it will not be smaller than the set Min Font Size. The following section is used to set how the keyboard is displayed. 13. Captitalization: Set to uppercase. The drop-down menu has four options: 13.1 None: do not set the upper case 13.2 Words: each word is capitalized, here the word refers to the string separated by spaces 13.3 Sentances: the first letter of each sentence is capitalized. The sentence here is a string separated by a period and a space: 13.4 All Characters: So the letter is capitalized 14. Correction: check spelling. The default value is YES. 15. Keyboard: select the Keyboard type, such as full numbers, letters, and numbers. 16. Appearance: 17. Return Key: select the Return Key. You can select Search, Return, and Done. 18. Auto-enable Return Key: If this option is selected, the Return Key is valid only when at least one character is entered in the text box. 19. Secure: when your text box is used as the password input box, you can select this option. The character is displayed as an asterisk. 1. alignment Horizontal Alignment 2. alignment Vertical Alignment 3. returns whether the input box of a BOOL value is Selected (Selected) Enabled (available) Highlighted (Highlighted). Only specific characters (BOOL) textField :( UITextField *) textField shouldChangeCharactersInRange :( nsange) can be entered) range replacementString :( NSString *) string {NSCharacterSet * cs; cs = [[NSCharacterSet characterSetWithCharactersInString: NUMBERS] invertedSet]; NSString * filtered = [string componentsSeparate DByCharactersInSet: cs] componentsJoinedByString: @ ""]; // The array is separated by cs, And the array is separated by @ ". BOOL canChange = [string isEqualToString: filtered]; return canChange;} The NUMBERS above is a macro, which can be defined at the top of the file: # define NUMBERS @ "0123456789n" (This indicates that NUMBERS and line breaks can be entered. Please note that n, if you do not enter this information, the Done button will not be triggered. If you use it in the SearchBar, it will not trigger the Search event, because you will not allow n input by yourself, I found it only in the project .) Therefore, if you want to restrict the input of English letters and numbers, you can define this as: # define kAlphaNum @ "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 ″. Of course, you can also make a prompt before the return method, such as prompting users to enter only numbers. If you need it. Only characters of a certain length can be entered-(BOOL) textField :( UITextField *) textField shouldChangeCharactersInRange :( nsange) range replacementString :( NSString *) string; {// string is the character textField that is input at this time. The input box that is being input at this time returns YES, which can change the value of the input box. if ([string isEqualToString: @ "n"]) // You Can Change {return YES;} NSString * toBeString = [textField. text stringByReplacingCharactersInRange: range withString: string]; // obtain the content of the input box if (self. myTextField = textField) // specifies the input box {if ([toBeString length]> 20) that we want to specify when determining whether the input box is used {// if the input box contains more than 20 content, a warning textField is displayed. 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] autorelles]; [alert show]; return NO ;}} return YES ;}

Related Article

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.