IOS basics-UITextField

Source: Internet
Author: User

IOS basics-UITextField

UITextField is usually used for external text input, which is a common input box. The following code describes various UITextField attributes and methods.

 

-(Void) viewDidLoad {[super viewDidLoad]; // text input box UITextField * textField = [[UITextField alloc] init]; // sets textField. frame = CGRectMake (50, 50,200,200); // set the text in the input box to empty textField by default. text = nil; // set the attribute string in the input box. The default value is null. If you do not understand the attribute string, you can skip textField. attributedText = nil; // sets the default attribute string textField. defaultTextAttributes = nil; // sets the default display attribute string textField. attributedPlaceholder = nil; // set the text color of the input box. The default value is null. Do not set it because the system is set to black textField. textColor = [UIColor blackColor]; // sets the background color textField. backgroundColor = [UIColor greenColor]; // set the font of the input box. The default value is null. Do not set it. The default value is 12pt textField. font = [UIFont systemFontOfSize: 18.0]; // set the background image only valid when the field type is set to UITextBorderStyleNone. // textField. background = [UIImage imageNamed: @ "1.png"]; // textField. disabledBackground = [UIImage imageNamed: @ "2.png"]; // set the input box to the password box. Each input character is changed to a vertex textField. secureTextEntry = NO;/* typedef NS_ENUM (NSInteger, NSTextAlignment) {bytes = 0, // left alignment # if iphone NSTextAlignmentCenter = 1, // center NSTextAlignmentRight = 2, // right aligned # else ipad NSTextAlignmentRight = 1, // always ally right aligned NSTextAlignmentCenter = 2, // Visually centered # endif other align = 3, // and paragraph alignment align = 4, // under normal conditions by default} NS_ENUM_AVAILABLE_IOS (6_0); * // sets the text display mode textField in the input box. textAlignment = NSTextAlignmentNatural; // sets the text 70% transparency textField displayed by default. placeholder = @ "Please input text";/* typedef NS_ENUM (NSInteger, UITextBorderStyle) {UITextBorderStyleNone, // no border UITextBorderStyleLine, // rectangular solid line trim, // rectangular solid line UITextBorderStyleRoundedRect // line with transparency in the rounded corner}; * // sets the textField of the outer frame type. borderStyle = UITextBorderStyleNone; // you can specify the minimum font value of textField in the input box. minimumFontSize = 18.0; // when it is 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 will scroll textField. adjustsFontSizeToFitWidth = NO;/* ypedef NS_ENUM (NSInteger, UIKeyboardType) {UIKeyboardTypeDefault, // default status, supports all characters UIKeyboardTypeASCIICapable, // you can enter the ASCII code limit, // UIKeyboardTypeURL, // letter and url (com) UIKeyboardTypeNumberPad, // UIKeyboardTypePhonePad, // number band + * # UIKeyboardTypeNamePhonePad, // you can enter the name UIKeyboardTypeEmailAddress On The Phone keyboard. // The keyboard @. enter the email UIKeyboardTypeDecimalPad NS_ENUM_AVAILABLE_IOS (4_1), // number key disk. UIKeyboardTypeTwitter extract (5_0), // includes the @ # UIKeyboardTypeWebSearch extract (7_0), // uses the bring button UIKeyboardTypeAlphabet = delimiter, // you can enter the upper Letter of the ASCII code }; * /// set the keyboard style textField. keyboardType = UIKeyboardTypeURL; // the content of the input box is cleared when the input box is edited again. clearsOnBeginEditing = NO; // whether the first letter is uppercase/* typedef NS_ENUM (NSInteger, UITextAutocapitalizationType) {lowercase, // all lowercase UITextAutocapitalizationTypeWords, // each word (with spaces in the middle) the first letter of UITextAutocapitalizationTypeSentences is capitalized, // The first letter of the first word is capitalized UITextAutocapitalizationTypeAllCharacters, // all uppercase}; */textField. autocapitalizationType = prop;/* Indicates whether there is a cross in the input box. When is it displayed? It is used to delete typedef NS_ENUM (NSInteger, UITextFieldViewMode) {UITextFieldViewModeNever, // no signature is displayed by default. // when the input is made, the cross sign UITextFieldViewModeUnlessEditing is displayed. // when the input is not made, the cross sign UITextFieldViewModeAlways is displayed all the time.}; */textField. clearButtonMode = optional;/* typedef enum {UITextAutocorrectionTypeDefault, default UITextAutocorrectionTypeNo, no automatic correction UITextAutocorrectionTypeYes, automatic correction} UITextAutocorrectionType; * // whether to correct textField. autocorrectionType = keys;/* typedef NS_ENUM (NSInteger, UIReturnKeyType) {UIReturnKeyDefault, // The return key displays the default status: return UIReturnKeyGo, // The return key displays Go UIReturnKeyGoogle, // Search UIReturnKeyJoin is displayed for the return key. // Join is added to UIReturnKeyNext for the return key. // The return key displays the next UIReturnKeyRoute for the next key. // The return key displays the Route UIReturnKeySearch, // The return key displays the Search UIReturnKeySend, // The return key displays the Send UIReturnKeyYahoo, // The return key displays the Search UIReturnKeyDone, // The return key creates the Done UIReturnKeyEmergencyCall ,}; * /// set the type of the return key textField. returnKeyType = UIReturnKeyDefault;/* typedef NS_ENUM (NSInteger, UIKeyboardAppearance) {optional, // default appearance: light gray; // dark gray graphite color; // light gray color = optional, // dark gray graphite color }; * /// set the keyboard appearance textField. keyboardAppearance = UIKeyboardAppearanceDefault; // set the view UIImageView * image = [[UIImageView alloc] initWithImage: [UIImage imageNamed: @ "1"]; image. bounds = CGRectMake (0, 0, 10, 10); textField. leftView = image; // The display status is similar to that of textField. leftViewMode = UITextFieldViewModeAlways; // set the view UIImageView * image1 = [[UIImageView alloc] initWithImage: [UIImage imageNamed: @ "2"] on the right of the input box; image1.bounds = CGRectMake (0, 0, 10, 10); textField. rightView = image1; // The display status is similar to that of textField. rightViewMode = UITextFieldViewModeAlways; textField. delegate = self; [self. view addSubview: textField];}/* override various painting methods // override to reset the Edge Area-(CGRect) borderRectForBounds :( CGRect) bounds; // override to reset the text area-(CGRect) textRectForBounds :( CGRect) bounds; // rewrite to reset the placeholder area, that is, the default display area-(CGRect) placeholderRectForBounds :( CGRect) bounds; // rewrite to reset the editing area-(CGRect) editingRectForBounds :( CGRect) bounds; // rewrite to reset the clearButton position. Changing the size may cause distortion of the button image-(CGRect) clearButtonRectForBounds :( CGRect) bounds; // rewrite to reset the Left view area-(CGRect) reset :( CGRect) bounds; // rewrite to reset the right view area-(CGRect) rightViewRectForBounds :( CGRect) bounds; // rewrite the painting to change the text attribute. When rewriting, super can be called to draw based on the default graphic attribute. If you completely rewrite the painting function, you do not need to call super-(void) drawTextInRect :( CGRect) rect; // rewrite the painting to change the placeholder attribute. During the rewriting, super can be called to draw based on the default graphic attribute. If you completely override the painting function, you do not need to call super-(void) drawPlaceholderInRect :( CGRect) rect; */# pragma mark-UITextFieldDelegate // set whether the input box can be modified. // NO, NO keyboard is displayed. // YES, default value: (BOOL) textFieldShouldBeginEditing :( UITextField *) textField {return YES;} // This method is executed when the focus is obtained when the edit is started-(void) textFieldDidBeginEditing :( UITextField *) textField {}/* returns the BOOL value, specifying whether to allow the contact of text fields. When the editing is complete, the text field will cause the first responder to stop the text field from disappearing when the user finishes editing, you can return "NO", which is useful for some programs whose text fields must always be active. For example, even if the message */-(BOOL) textFieldShouldEndEditing :( UITextField *) textField {return NO ;} // after the text box is edited, the system calls-(void) textFieldDidEndEditing :( UITextField *) textField {}/*. When you use the automatic correction function to change the input text to the recommended text, this method is called. This method is especially useful for applications that want to add the Undo option. You can trace the last modification made in the field or record all edits, to prevent text changes, another nsange object in the parameter "NO" can be returned, indicating the location of the changed text, the modified text is also recommended */-(BOOL) textField :( UITextField *) textField shouldChangeCharactersInRange :( nsange) range replacementString :( NSString *) string {return YES ;} // 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-(BOOL) textFieldShouldClear :( UITextField *) textField {return YES;} // return a BOOL value, indicating whether to allow end editing when the Enter key is pressed. // If the resignFirstResponder method is allowed, the edit is terminated, while the keyboard will be angry-(BOOL) textFieldShouldReturn :( UITextField *) textField {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.