Summary of common properties for UITextField components in iOS App development

  • 2020-05-30 21:08:35
  • OfStack

The key attributes

Use IB in Xcode to drag up a textbox for the view, select the textbox, and you can set its various properties in Attribute Inspector.

Attribute Inspector is divided into three parts: Text Field, Control and View. Let's focus on the Text Field section.

The Text Field section has the following options:

1. Text: set the default text of the text box.

2. Placeholder: gray words can be displayed in the text box to prompt the user what to enter in the text box. When data is entered into the text box, the grey word used for the prompt will automatically disappear.

3, Background:

4. Disabled: if this is selected, the user will not be able to change the text box content.

5. Next are three buttons to set the alignment.

6. Border Style: choose a border style.

7, Clear Button: this is a drop-down menu, you can choose when the clear button will appear, the so-called clear button is a small X to the right of the text box, you can choose as follows:

7.1 Never appears: never show up
7.2 Appears while editing: appears when editing
7.3 Appears unless editing:
7.4 Is always visible: always visible

8. Clear when editing begins: if this is selected, the text box will be cleared when editing begins. For example, if you type "What" into the text box A, then go to edit the text box B. If you go back to edit the text box A, the "What" will be cleared immediately.

9. Text Color: sets the color of Chinese text in the text box.

10. Font: sets the font and size of the text.

11. Min Font Size: set the smallest font that you can display in the text box (I don't think that's useful, though)

12. Adjust To Fit: specifies whether the text in the text box should also be shrunk when the size of the text box is reduced. Select it so that all text is visible, even if the text is long. However, this option works with Min Font Size, and the text is no smaller than Min Font Size.

The next section sets how the keyboard is displayed.

13. Captitalization: set uppercase. There are four options in the drop-down menu:

13.1 None: no uppercase
13.2 Words: each word begins with a capital letter. The word in this case refers to a string separated by Spaces
13.3 Sentances: the first letter of each sentence is capitalized. In this case, the sentence is a string separated by a period and a space
13.4 All Characters: so capital letters

14. Correction: spell check; the default is YES.

15. Keyboard: select the keyboard type, such as full Numbers, letters and Numbers.

16, Appearance:

17. Return Key: select the return key to select Search, Return, Done, etc.

18, Auto-enable Return Key: if this is selected, only the return key of the keyboard is valid after at least 1 character has been entered in the textbox.

19. Secure: you can select this option when your text field is used as a password entry field. At this point, the characters appear as asterisks.

skills

Here are two tips:
1. UITextField hidden keyboard

1. Click return on the keyboard to hide the keyboard

This method requires the implementation of the UITextFieldDelegate protocol in the corresponding.h file. Add the following method to the.m file


- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}

2. Click the interface space to hide the keyboard

The implementation of this method is mainly to add the click event to the current view, without adding the corresponding processing method to the click event. This is to hide the keyboard, so we can ask UITextField to give up the first responder in the method corresponding to the click event.


- (void)dismissKeyboard
{
    NSArray *subViews = [self.view subviews];
    for (id inputText in subViews) {
        if ([inputText isKindOfClass:[UITextField class]]) {
            if ([inputText isFirstResponder]) {
                [inputText resignFirstResponder];
            }
        }
    }
}

Adds a click event to the current view

UITapGestureRecognizer *dismissKeyboardTap = [[UITapGestureRecognizer alloc]                                                initWithTarget:self                                                 action:@selector(dismissKeyboard)];
[self.view addGestureRecognizer: dismissKeyboardTap];

2. Add validation to the content


- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
    switch (textField.tag) {
        case 100://name
        {
            NSLog(@"this is nameField");
            // Add the check name The code of
            break;
        }
        case 101://phone
        {
            NSLog(@"this is phoneField");
            // Add the check phone The code of
            break;
        }
        case 102://email
        {
            NSLog(@"this is emailField");
            // Add the check email The code of
            break;
        }       
        default:
            break;
    }
    return YES;
}


Related articles: