iOS開發之UITextField

UITextField展現的是一些可編輯的內容,而且與用戶有一些交互。好比當你在虛擬鍵盤上按下return鍵時,通常會關聯到鍵盤隱藏事件上。UITextField的一些狀態大多在UITextFieldDelegate協議中有相應的方法。html

####UITextField的初始化及一些屬性####ios

//姓名輸入域
UITextField *nameField = [[UITextField alloc] initWithFrame:CGRectMake(30, 30, 200, 44)];
nameField.tag = 100;
nameField.delegate = self;
    //默認文字
    nameField.placeholder = @"name";
    nameField.font = [UIFont systemFontOfSize:16.0f];
    nameField.textColor = [UIColor blackColor];
    //輸入框的背景圖片(還能夠選擇設置背景顏色)
    nameField.background = [UIImage imageNamed:@"textFieldBackgroundImage"];
    //nameField.backgroundColor = [UIColor lightGrayColor];
    //清除按鈕
    nameField.clearButtonMode = UITextFieldViewModeAlways;
    //鍵盤類型
    nameField.keyboardType = UIKeyboardTypeDefault;
    [self.view addSubview:nameField];
    電話輸入域
    UITextField *phoneField = [[UITextField alloc] initWithFrame:CGRectMake(30, nameField.frame.origin.y + nameField.bounds.size.height+10, 200, 44)];
    phoneField.tag = 101;
    phoneField.delegate = self;
    phoneField.placeholder = @"phone";
    phoneField.keyboardType = UIKeyboardTypeDecimalPad;
    phoneField.clearButtonMode = UITextFieldViewModeAlways;
    [self.view addSubview:phoneField];
    //郵箱輸入域
    UITextField *emailField = [[UITextField alloc] initWithFrame:CGRectMake(30, phoneField.frame.origin.y + phoneField.bounds.size.height + 10, 200, 44)];
    emailField.tag = 102;
    emailField.delegate = self;
    emailField.placeholder = @"email";
    emailField.keyboardType = UIKeyboardTypeEmailAddress;
    emailField.clearButtonMode = UITextFieldViewModeAlways;
    [self.view addSubview:emailField];

####UITextField隱藏鍵盤#### 1.點擊鍵盤的return來隱藏鍵盤app

這個方法須要在相應的.h文件文件中實現UITextFieldDelegate協議。並在.m文件中添加以下方法this

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

2.點擊界面空白處來隱藏鍵盤code

這個方法的實現主要是給當前的view增長點擊事件,並未點擊事件增長相應的處理方法,此處是爲了隱藏鍵盤,因此咱們能夠在點擊事件對應的方法中讓UITextField放棄第一響應者。htm

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

爲當前的view增長點擊事件事件

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

####UITextField--爲內容增長校驗####圖片

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
    switch (textField.tag) {
        case 100://name
        {
            NSLog(@"this is nameField");
            //添加校驗name的代碼
            break;
        }
        case 101://phone
        {
            NSLog(@"this is phoneField");
            //添加校驗phone的代碼
            break;
        }
        case 102://email
        {
            NSLog(@"this is emailField");
            //添加校驗email的代碼
            break;
        }        
        default:
            break;
    }
    return YES;
}
相關文章
相關標籤/搜索