常用属性
@property (nonatomic) UITextBorderStyle borderStyle;
@property (nullable, nonatomic,strong) UIImage *background;
@property (nonatomic,copy) NSString *placeholder;
@property (nonaatomic,copy) NSString *text;
@property (nonatomic,strong) UIColor *textColor;
@property (nonatomic,strong) UIFont *font;
@property (nonatomic,assign) NSTextAlignment textAlignment;
@property (nonatomic,weak) id<UITextFieldDelegate> delegate;
@property (nonatomic,assign) UIKeyboardType keyboardType;
@property (nonatomic,assign) UIKeyboardAppearance keyboardAppearance;
@property (nonatomic,assign) UIReturnKeyType returnKeyType;
@property(nonatomic,getter=isSecureTextEntry)BOOL secureTextEntry;
@property (nonatomic,assign) UITextFieldViewMode clearMode;
@property (nonatomic,strong) UIView *leftView;
leftViewMode
@property(nonatomic) UITextFieldViewMode leftViewMode;
示例代码
1.UIImageView *leftView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
2.leftView.image = [UIImage imageNamed:@"pied_piper_2"];
3.textField.leftView = leftView;
4.textField.leftViewMode = UITextFieldViewModeAlways;
5.
@property (nonatomic,strong) UIView *rightView;
rightViewMode
@property(nonatomic) UITextFieldViewMode rightViewMode;
示例代码
1.UIImageView *rightView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
2.rightView.image = [UIImage imageNamed:@"crystal_ball"];
3.textField.rightView = rightView;
4.textField.rightViewMode = UITextFieldViewModeAlways;
5.
1.
2.// 清除按钮回调
3.-(BOOL)textFieldShouldClear:(UITextField *)textField{
4. return YES;
5.}
6.
7.// 返回键回调
8.-(BOOL)textFieldShouldReturn:(UITextField *)textField{
9. return YES;
10.}
11.
12.-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
13. // 此方法是在对象申请注册成为firstresponder时(也就是[textField becomeFirstResponder]时)调用
14. return YES;
15.}
16.
17.-(void)textFieldDidBeginEditing:(UITextField *)textField{
18. // 此方法实在对象注册成为 firstresponder 后调用
19.}
20.
21.-(BOOL)textFieldShouldEndEditing:(UITextField *)textField{
22. // 此方法是在 [textField resignFirstResponder] 时调用
23. return YES;
24.}
25.
26.-(void)textFieldDidEndEditing:(UITextField *)textField{
27. // 此方法是在 textFiled 辞去 FirstResponder 后 调用
28.}
29.
30.-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
31. // 获取实时输入的内容
32. // 此方法是在键盘点击输入 string 并且在将 string 追加到文本框之前调用
33. // 参数 string 文本框的内容
34. // range.location 表示光标的位置.光标永远在文本内容的最末尾(在不主动移动光标的情况下)
35. return YES;
36.}
37.
38.
原文:http://www.cnblogs.com/buakaw/p/5072780.html