IOS stage study 29th day notes (UITextField Introduction), iosuitextfield

Source: Internet
Author: User

IOS stage study 29th day notes (UITextField Introduction), iosuitextfield

IOS Learning (UI) knowledge point sorting

1. Introduction to UITextField

1) concept: UITextField is a control used to receive user input.

2) UITextField instance initialization code:

1 // create a UItextField instance 2 UITextField * textField = [[UITextField alloc] init]; 3 textField. frame = CGRectMake (10, 40, self. view. frame. size. width-20, 40); 4 textField. backgroundColor = [UIColor lightGrayColor]; 5 // set the text 6 textField in textFiled. text = @ "username"; 7 // set textFiled font 8 textField. font = [UIFont systemFontOfSize: 20]; 9 // set textFiled text color to 10 textField. textColor = [UIColor purpleColor]; 11 [self. view addSubview: textField];


3) textAlignment:

1 // set the left alignment of textFiled text 2 textField. textAlignment = NSTextAlignmentLeft;

 

4) borderStyle:

1 // set textFiled style to borderless style 2 textField. borderStyle = UITextBorderStyleNone; 3 // borderStyle has the following types: 4 // 1, UITextBorderStyleNone, 5 // 2, UITextBorderStyleLine, 6 // 3, snapshot, 7 // 4, Snapshot


5) layer. cornerRadius:

1  textField.layer.cornerRadius = 4.0f;

 

6) layer. borderWidth:

1 textField.layer.borderWidth = 1;

 

7) layer. borderColor:

1  textField.layer.borderColor = [UIColor darkGrayColor].CGColor;


8) set the background image of the text box as follows:

1 UIImage *image = [UIImage imageNamed:@"btnEmojBtn"];2  textField.background = image;


9) set the text box to display text by default (the text disappears when the default gray font is clicked on the keyboard) for example:
Method 1: placeholder

1 textField. placeholder = @ "username ";

Method 2: NSMutableAttributedString

1 Optional * muAttStr = [[NSMutableAttributedString alloc] initWithString: @ "username"]; 2 [muAttStr addAttribute: invalid value: 3 [UIColor blueColor] range: NSMakeRange (0, muAttStr. length)]; 4 textField. attributedPlaceholder = muAttStr;


10) clearButtonMode sets the clear content button display mode of the text box (that is, the cross on the right of the text box) for example:

TextField. clearButtonMode = UITextFieldViewModeWhileEditing;
Typedef enum {UITextFieldViewModeNever, // never appears. // when editing, the cursor appears. // UITextFieldViewModeAlways appear except for editing.} UITextFieldViewMode;


11) leftView:

1 UIView * leftView = [[UIView alloc] init]; 2 leftView. backgroundColor = [UIColor clearColor]; 3 leftView. frame = CGRectMake (0, 0, 50, 40); 4 textField. leftView = iconImgView; 5 // set the display mode of the left-side view of the text box to 6 textField. leftViewMode = UITextFieldViewModeAlways;

 

12) returnKeyType: Set the button type in the lower right corner of the keyboard in the text box. For example:

1 textField.returnKeyType = UIReturnKeyDone;

 

13) userInteractionEnabled: Set whether the text box can be edited. For example:

1 // You Cannot edit the text box. 2 textField. userInteractionEnabled = NO;

 

14) becomeFirstResponder:

1 [textField becomeFirstResponder]; 2 // Note: When a control is set as the first responder, the other one is set as the first responder. The first responder has only one and only one

 

15) resignFirstResponder sets the text box to discard the first responder to hide the keyboard, for example:

1 [textField resignFirstResponder];

 

16) secureTextEntry: Set the text box ciphertext mode, that is, the password box, for example:

1  textField .secureTextEntry = YES;


17) keyboardType:

1 // set the numeric keypad 2 textField. keyboardType = UIKeyboardTypeNumberPad; 3 4 // keyboardType: 5 typedef enum {6 UIKeyboardTypeDefault, // default keyboard, supports all characters 7 UIKeyboardTypeASCIICapable, // supports ASCII default keyboard 8 Tib, // standard phone keyboard, supporting + * # character 9 UIKeyboardTypeURL, // URL keyboard, supported. the com button only supports the URL character 10 UIKeyboardTypeNumberPad, // The keyboard 11 UIKeyboardTypePhonePad, // The telephone keyboard 12 UIKeyboardTypeNamePhonePad, // The telephone keyboard, and the name 13 UIKeyboardTypeEmailAddress, // enter the email address on the keyboard 14 UIKeyboardTypeDecimalPad, // The numeric keyboard has a number and a decimal point 15 UIKeyboardTypeTwitter, // The optimized keyboard facilitates the input of @, # character 16 UIKeyboardTypeAlphabet = random, 17} UIKeyboardType;

 

18) adjustsFontSizeToFitWidth: when the text box content exceeds the text box range, the text automatically changes. For example:

1 textField. adjustsFontSizeToFitWidth = YES; 2 // you can specify the minimum font size for autoscaling down. 3 textField. minimumFontSize = 20;

 

19) keyboardAppearance:

// Dark gray graphite keyboard 2 textField. keyboardAppearance = UIKeyboardAppearanceAlert; 3 typedef enum {4 UIKeyboardAppearanceDefault, // default appearance, light gray 5 Tib, // dark gray graphite 6} UIReturnKeyType;

 

20) text box content alignment, for example:

1 // The vertical alignment of content UITextField inherits from UIControl. This class has an attribute contentverticalignment2 textField. contentVerticalAlignment = uicontrolcontentverticalignmentcenter;

 

21) font: Set the text box font style and size, for example:

1   textField.font = [UIFont fontWithName:@"Arial" size:20.0f];


22) the delegate setting text box proxy object is as follows:

TextField. delegate = self; // when setting proxy in the text box, the current class must first comply with the UITextFieldDelegate protocol and then implement its Protocol Methods // Its Protocol methods include: // return a BOOL value, whether to allow the text box to start editing 1.-(BOOL) textFieldShouldBeginEditing :( UITextField *) textField; // triggered when editing starts. The text box will become the first responder (first responder) 2.-(void) textFieldDidBeginEditing :( UITextField *) textField; // return the BOOL value, whether to allow the text box to end editing. When the editing is complete, the current text box will discard the first responder 3,-(BOOL) textFieldShouldEndEditing :( UITextField *) textField; // This method is triggered when the text box is edited. 4.-(void) textFieldDidEndEditing :( UITextField *) textField; // This method is triggered when the text box content changes. You can filter illegal characters or restrict input 5,-(BOOL) textField :( UITextField *) textFieldshouldChangeCharactersInRange :( nsange) in this method) range replacementString :( NSString *) string; // This method is triggered when you click the Clear button on the right of the text box. 6.-(BOOL) textFieldShouldClear :( UITextField *) textField; // This method is triggered when you click Return in the lower-right corner of the keyboard. 7.-(BOOL) textFieldShouldReturn :( UITextField *) textField;

 

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.