標籤:
UITextField->UIControl->UIView
常用屬性
1.圖片對象轉化為顏色對象
textField.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@”DOVE 1”]];
2.borderStyle 邊框樣式
textField.borderStyle = UITextBorderStyleNone;
3.contentVerticalAlignment 文本垂直方向
textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
4.adjustsFontSizeToFitWidth 自適應文字大小
textField.adjustsFontSizeToFitWidth = YES;
5.minimumFontSize 最小字型
textField.minimumFontSize = 18;
6.keyboardAppearance 鍵盤外觀
textField.keyboardAppearance = UIKeyboardAppearanceDark;
7.keyboardType 鍵盤樣式
textField.keyboardType = UIKeyboardTypeDefault;
8.textColor 文字顏色
textField.textColor = [UIColor purpleColor];
9.returnKeyType return鍵樣式
textField.returnKeyType = UIReturnKeyGo;
10.autocapitalizationType 字母大寫樣式
textField.autocapitalizationType =UITextAutocapitalizationTypeAllCharacters;
11.autocorrectionType 自動錯誤修正
textField.autocorrectionType = NO;
12.placeholder 提示文字
textField.placeholder = @”屠龍寶刀,點擊就送”;
13.secureTextEntry 是否密文顯示
textField.secureTextEntry = YES;
14.clearButtonMode 一鍵刪除的模式
textField.clearButtonMode = UITextFieldViewModeWhileEditing;
15.clearsOnBeginEditing 再次編輯是否清空
textField.clearsOnBeginEditing = YES;
16.leftView 左視圖
textField.leftView = view;
17.leftViewMode 左視圖方式
textField.leftViewMode = UITextFieldViewModeAlways;
18.rightView 右視圖
19.rightViewMode 右視圖方式
20.inputAccessoryView 鍵盤上面的視圖
21.inputView 鍵盤樣式
22.textfield的代理
/**
* 輸入框是否可以開始輸入文字
*
* @param textField
*
* @return NO表示不能輸入,YES表示可以輸入
*/
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
NSLog(@”%s”,func);
return YES;
}// return NO to disallow editing.
/**
* 輸入框開始響應就執行
*
* @param textField
*/
- (void)textFieldDidBeginEditing:(UITextField *)textField{
NSLog(@”%s”,func);
}// became first responder
//輸入框是否可以結束輸入文字
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField{
// if (textField.text.length !=2) {
// return NO;
// }
NSLog(@”%s”,func);
return YES;
}// return YES to allow editing to stop and to resign first responder status. NO to disallow the editing session to end
/**
* 輸入框結束響應就執行
*
* @param textField
*/
- (void)textFieldDidEndEditing:(UITextField *)textField{
NSLog(@”%s”,func);
}// may be called if forced even if shouldEndEditing returns NO (e.g. view removed from window) or endEditing:YES called
//
/**
* 輸入框文字出現變化就執行的方法
*
* @param textField
* @param range
* @param string
*
* @return
*/
- (BOOL)textField:(UITextField )textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString )string{
NSLog(@"%ld %ld",range.length,range.location);NSLog(@"%@",string);return YES;
}// return NO to not change text
/**
* 一鍵刪除
*
* @param textField
*
* @return NO表示刪除無效
*/
- (BOOL)textFieldShouldClear:(UITextField *)textField{
if ([textField.text isEqualToString:@”123”]) {
return NO;
}
NSLog(@”%s”,func);
return YES;
}// called when clear button pressed. return NO to ignore (no notifications)
/**
* textFieldShouldReturn
*
* @param textField 傳入的鍵盤
*
* @return
*/
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
//收回鍵盤:游標消失 輸入框失去第一響應
[textField resignFirstResponder];
NSLog(@”%s”,func);
return YES;
}// called when ‘return’ key pressed. return NO to ignore.
iOS學習UI之UITextfield