UITextField
-
常用属性
- 边框样式
@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;
- leftView (需要设置leftViewMode)
@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.
-
- rightView(需要设置rightViewMode,显示了rightView之后清除按钮将不会显示)
@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.