UITextField文本框

1.常用属性

  • 设置文本内容
@property(nonatomic,copy) NSString *text;
  • 设置文本内容颜色
@property(nonatomic,retain) UIColor *textColor;
  • 设置字体大小
@property(nonatomic,retain) UIFont *font;
  • 设置边框样式
@property(nonatomic) UITextBorderStyle borderStyle;

typedef enum {
    UITextBorderStyleNone,
    UITextBorderStyleLine,
    UITextBorderStyleBezel,
    UITextBorderStyleRoundedRect
} UITextBorderStyle;
  • 设置对齐方式
@property(nonatomic) UITextAlignment textAlignment;
  • 设置提示内容
@property(nonatomic,copy) NSString *placeholder;
  • 用户编辑时是否清楚内容,默认NO
@property(nonatomic) BOOL clearsOnBeginEditing;
  • 自适应调整字体大小
@property(nonatomic) BOOL adjustsFontSizeToFitWidth;
  • 设置代理(重要)
@property(nonatomic,assign) id UITextFieldDelegate delegate;
  • 设置背景
@property(nonatomic,retain) UIImage *background;
  • 光标颜色
@property(nonatomic, strong) UIColor *tintColor;
  • 清除按钮设计模式,默认不出现
@property(nonatomic) UITextFieldViewMode clearButtonMode;
  • 是否安全输入
@property(nonatomic) BOOL secureTextEntry;
  • 左边的视图以及样式
@property(nonatomic,strong) UIView  *leftView; 

@property(nonatomic) UITextFieldViewMode  leftViewMode;

typedef enum {
    UITextFieldViewModeNever,  //默认
    UITextFieldViewModeWhileEditing,
    UITextFieldViewModeUnlessEditing,
    UITextFieldViewModeAlways
} UITextFieldViewMode;
  • 右边的视图以及样式
@property(nonatomic,strong) UIView  *rightView; 

@property(nonatomic) UITextFieldViewMode  rightViewMode;

typedef enum {
    UITextFieldViewModeNever,  //默认
    UITextFieldViewModeWhileEditing,
    UITextFieldViewModeUnlessEditing,
    UITextFieldViewModeAlways
} UITextFieldViewMode;
  • 自定义键盘
@property(nonatomic,retain) UIView  *inputView;
  • 自定义和系统共存的键盘
property(nonatomic,retain) UIView  *inputAccessoryView;
  • 修改键盘类型
property(nonatomic) UIKeyboardType  *keyboardType;

2.代码例子

    //创建
    UITextField *tf = [[UITextField alloc] initWithFrame:CGRectMake(60, 180, 200, 35)];
    //样式
    tf.borderStyle = UITextBorderStyleRoundedRect;
    //提示内容
    tf.placeholder = @"请输入相关内容";
    //是否安全输入
    tf.secureTextEntry = YES;
    //设置背景
    tf.backgroundColor = [UIColor grayColor];
    //添加
    [self.view addSubview: tf];
运行结果

3.代理设计

要使用UITextField,我们要采用代理,设置代理,然后使用代理的方法

  • 采用代理
@interface ViewController ()<UITextFieldDelegate>
  • 设置代理
  tf.delegate = self;

方法

  • 在应该编辑前调用
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    //在这里写应该编辑前要做的事情
    return  YES;
    //返回值为YES就默认打开第一响应[tf becomeFirstResponder]
}
  • 在编辑前调用
- (void)textFieldDidBeginEditing:(UITextField *)textField{
    //在这里写编辑前要做的事
}
  • 在应该结束编辑前调用
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField{
    //这里写应该结束编辑前要做的事
    return YES;
    //返回值为YES就默认关闭第一响应[tf resignFirstResponder]
}
  • 在编辑时调用
-(void)textFieldDidEndEditing:(UITextField *)textField{
    //这里写编辑结束后要做的事
}
  • 在编辑中调用
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    //这里写编辑中要做的事
    return  YES;
    //返回值为YES就在每编辑一下调用一下这个方法,若返回为NO则不能编辑
}
  • 在点击清除按钮时要做的事
- (BOOL)textFieldShouldClear:(UITextField *)textField{
    //这里写点击清除按钮时要做的事
    return YES;
}
  • 在点击键盘return键时要做的事
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    //这里写点击键盘return时要做的事
    return YES;
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容