[IOS development-15] common UITextField attributes include text box style, text style, keyboard style, left and right view style, and clear button settings. iosuitextfield

Source: Internet
Author: User

[IOS development-15] common UITextField attributes include text box style, text style, keyboard style, left and right view style, and clear button settings. iosuitextfield

(1) You can set the text box style (including shape, border color, background, etc.) as needed ).


(2) you can set the text display style as needed (including the ciphertext display when the password is entered, the text is horizontally centered, vertically centered up or down, whether the entered text is capitalized, whether the text is scaled down or rolled to the right after it is exceeded ).


(3) You can set different keyboard styles (only numbers, letters, and so on) as needed ).


(4) there is also an inputView that can pop up a view to replace the pop-up keyboard. I don't know what the use is, but it seems that I can use a lot of it.


(5) There are also return style settings, which can be set to Google, Go, Search, and other more image buttons.


(6) There is also a clearsOnBeginEditing whether to set the Clear button.


(7) It is also useful to estimate the left and right views, that is to say, there is a small icon image in front of our common user name and password to indicate the user's "villain" and the "Lock" image indicating the password. It can be loaded in the left and right views, remember to set the left and right view mode to Always. Otherwise, the default view is not displayed by Never.


-(Void) viewDidLoad {// textfiled1 Was Supposed To Be textField1, but it does not affect UITextField * textFiled1 = [[UITextField alloc] init]; // textField1 already exists at this time, however, because the background is transparent, you cannot see it. However, you can find that the cursor blinks and can be written. // to prove that the background is transparent rather than a white background, you can set self. the view background is red. Check whether textField1 is white or transparent. // self. view. backgroundColor = [UIColor redColor]; textFiled1.frame = CGRectMake (10, 30,300, 30); // set the border style // UITextBorderStyleRoundedRect-rounded rectangle with a white background, no longer transparent // UITextBorderStyleLine-rectangle, black border, Transparent background // UITextBorderStyleBezel-it is similar to the above, but it is a gray border. The background is transparent textFiled1.borderStyle = UITextBorderStyleRoundedRect; // you can specify the background color, it will overwrite the default white background textFiled1.backgroundColor = [UIColor purpleColor] of the rounded rectangle above; // The prompt text textFiled1.placeholder = @ "enter your password "; // set the ciphertext input, which is displayed as a small dot textFiled1.secureTextEntry = YES when entering the password; // set the keyboard style. For example, the bank withdrawal password only needs a number, some input mailboxes require @ and so on // UIKeyboardTypeAlphabet and UIKeyboardTypeDefault are similar, that is, we usually see that, are letters, and then there is a button to switch the symbol/ /UIKeyboardTypeASCIICapable seems to be similar to the above. // UIKeyboardTypeDecimalPad and UIKeyboardTypeNumberPad are both numbers, however, the former has an additional "decimal point" button // UIKeyboardTypeEmailAddress-in addition to letters, there are also decimal points and @/UIKeyboardTypeNamePhonePad-seemingly normal // UIKeyboardTypePhonePad-telephone keyboard, there are not only numbers but also the types of * and # // UIKeyboardTypeNumbersAndPunctuation-only numbers and punctuations/UIKeyboardTypeTwitter-besides letters and @ and #, which are microblog symbols/UIKeyboardTypeURL-in addition to letters, and. com button to facilitate the input // UIKeyboardTypeWebSearch-the main difference is that the return key is changed to the GO key // Note: if it is an xcode6 simulator, the Keyboard is not called up by default. Press CMD + K to call it up, or set textFiled1.keyboardType = UIKeyboardTypeWebSearch In the Keyboard in the Hardware menu; // set the keyboard appearance. // The Difference Between the keyboard background and the keyboard background is not obvious. // The Difference Between the keyboard background and the keyboard background is not clear. // The difference is similar to that of the UIKeyboardAppearanceDefault. The difference is that textFiled1.keyboardAppearance =, inputView is not displayed on the keyboard, but on the frame of the view // set, only the height is useful. Other values x and y are invalid. The width is the default keyboard width. UIImageView * imgView1 = [[UIImageView alloc] initWithImage: [UIImage imageNamed: @ "logo-60@3x.png"]; imgView1.frame = CGRectMake (60, 60,300,300); textFiled1.inputView = imgView1; // set the Left view, that is, the user name and password. Sometimes, place an image in UIView * view1 = [[UIView alloc] init]; // The values of x and y are invalid, and the values of x are 0, y is automatically adjusted based on the height. That is, if the height is greater than textField, the default value is textField height. If the height is smaller than textField, the center is displayed. The only valid value is the width of view1.frame = CGRectMake (10,500, 50, 10); view1.backgroundColor = [UIColor orangeColor]; textFiled1.leftView = view1; // the most important time: by default, it is not displayed, that is, UITextFieldViewModeNever. We can set to always display UITextFieldViewModeAlways // UITextFieldViewModeUnlessEditing-there is a click box at the very beginning, click textFiled1.leftViewMode = UITextFieldViewModeAlways; // Similarly, you can set the right view. Of course, you can also load and image in UIImageView * imgView2 = [[UIImageView alloc] initWithImage: [UIImage imageNamed: @ "logo-60@3x.png"]; imgView2.frame = CGRectMake (10,500, 50, 10); textFiled1.rightView = imgView2; Preview = finished; // set the Clear button, which is the cross X. When you click the text in the entire input box, delete all the re-input X (we will comment out the text so that it will not be displayed in the right view to check the effect) // In fact, when we write clearButtonMode, it prompts That This Is A UITextFieldViewMode type, so it is also the same as the above textFiled1.clearButtonMode = UITextFieldViewModeWhileEditing; // whether to clear it when editing again Content, which is rarely used except for specific scenarios, will drive users crazy. // Of course, to simulate re-editing, we need to point the mouse to other places and then click back, create another textField textFiled1.clearsOnBeginEditing = NO; // This clearsOnInsertion seems to be unclear when you click back to edit it again, but the previous textFiled1.clearsOnInsertion = YES is cleared as long as you input the content; UITextField * textField2 = [[UITextField alloc] init]; textField2.frame = CGRectMake (10, 80,300,100); textField2.borderStyle = UITextBorderStyleRoundedRect; [self. view addSubview: textField2]; // use the textField created above 2. perform the following // vertical alignment. The default value is center. // uicontrolcontentverticalignmentcenter. Therefore, Top and Bottom are the Top and Bottom layers. Fill seems to be similar to Top textField2.contentVerticalAlignment = uicontrolcontentverticalignmentfill; // Of course there are also horizontal alignment // there are four types of left-middle-right and Fill, but it does not seem to have any effect, because there are special settings for text, such as align = align; // when we input textAlignment, We will prompt NSTextAlignment type. There are several types, not to mention textField2.textAlignment = NSTextAlignmentCenter; // set and adjust the text size to adapt to the width (that is, if the text cannot be reduced when input fails, it will scroll back). The default value is textField2.adjustsFontSizeToFitWidth = YES; // set the minimum font size, which is related to the above, that is, when the font size is smaller than this, I will not shrink it, and scroll textField2.minimumFontSize = 2 directly to the right; // set the letter size style, when you enter autocapitalizationType, a message is displayed: UITextAutocapitalizationType // UITextAutocapitalizationTypeAllCharacters-all uppercase letters (if you enter it on a keyboard, it is valid only when you enter it on a soft keyboard. The same applies to the following) // UITextAutocapitalizationTypeWords-UPPERCASE letters/UITextAutocapitalizationTypeSentences-UPPERCASE letters textField2.autocapitalizationType = UITextAutocapitalizationTypeSentences; // set the return style, there are Done/Go/Next/Join/Google/Search/Yahoo/EmergencyCall/Send and so on. Except for the default, other buttons are in the blue background textField2.returnKeyType = UIReturnKeyEmergencyCall; [self. view addSubview: textFiled1]; [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib .}



In ios development, how to use code or other methods to control the keyboard Switching now has two text boxes in the view, which are used to enter text and numbers, such

Tag your corresponding text box tag, and judge the tag value in the method of the text box to be edited.
-(BOOL) textFieldShouldBeginEditing :( UITextField *) textField;
Set different keyboard types based on the corresponding tag

In IOS software development, how does UITextField cancel the pop-up system keyboard?

Why must we focus? On your own keyboard, you can press the key to modify textField. text.

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.