iOS控件 ----- UITextField

UITextField

相關屬性的設置

//邊框樣式
textField.borderStyle = UITextBorderStyleRoundedRect;

//變爲第一響應者 自動彈出鍵盤
    [textField becomeFirstResponder];
//這是文本框清除按鈕的樣式
    textField.clearButtonMode = UITextFieldViewModeWhileEditing;
//默認顯示的文字
    textField.placeholder = @"請輸入文字";
//輸入密碼時的樣式 默認是NO
    textField.secureTextEntry = YES;
//設置鍵盤的樣式
    textField.keyboardType = UIKeyboardTypeDefault;
//返回鍵的樣式
    textField.returnKeyType = UIReturnKeyNext;
//文字樣式 垂直居中顯示
    textField.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;



    UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 320, 40)];
    label.backgroundColor = [UIColor blackColor];
    //設置二級鍵盤
    textField.inputAccessoryView = label;

收回鍵盤的方法

需要遵守UITextFieldDelegate協議
//設置代理
textField.delegate = self;

//點擊return鍵,返回爲yes
- (BOOL)textFieldShouldReturn:(nonnull UITextField *)textField
{
    //收起鍵盤
    [textField resignFirstResponder];
    //結束編輯也可以收起鍵盤
    //[self.view endEditing:YES];
    return YES;
}
//已經開始編輯,注意界面的變化,一定情況下界面要整體上移
- (void)textFieldDidBeginEditing:(nonnull UITextField *)textField
{
    NSLog(@"%s",__func__);
}


//結束編輯時,注意界面的變化,還原界面
- (void)textFieldDidEndEditing:(nonnull UITextField *)textField
{
    NSLog(@"%s",__func__);
}

//利用這個方法可以做到,點擊界面上除了輸入框之外的任何元素,收起鍵盤
- (void)touchesBegan:(nonnull NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event
{
    UITextField * textField = (UITextField *)[self.view viewWithTag:100];
    [textField resignFirstResponder];
    [self.view endEditing:YES];
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章