文本输入控件UITextField
一,作用:用户输入文字
@property(nonatomic,copy) NSString *text;
// 设置文本内容颜色
@property(nonatomic,retain) UIColor *textColor;
// 设置字体
@property(nonatomic,retain) UIFont *font
// 对齐样式
@property(nonatomic) NSTextAlignment textAlignment;
// 设置风格,默认没有风格,需要设置
@property(nonatomic) UITextBorderStyle borderStyle;
// 提示用户输入内容文本
@property(nonatomic,copy) NSString *placeholder;
// 用户编辑时是否clear内容,默认为NO
@property(nonatomic) BOOL clearsOnBeginEditing;
// 自适应调整字体大小,默认为NO
@property(nonatomic) BOOL adjustsFontSizeToFitWidth;
三,常用属性
// 设置代理
@property(nonatomic,assign) id<UITextFieldDelegate> delegate;
// 设置背景,需要将textField实例的风格设置为None
@property(nonatomic,retain) UIImage *background;
// 设置textField不可用时的背景图片
@property(nonatomic,retain) UIImage *disabledBackground;
// 设置是否可编辑
@property(nonatomic,readonly,getter=isEditing) BOOL editing;
// 清除按钮的模式,默认不出现
@property(nonatomic) UITextFieldViewMode clearButtonMode;
// 自定义左视图
@property(nonatomic,retain) UIView *leftView;
// 自定义左视图出现的模式
@property(nonatomic) UITextFieldViewMode leftViewMode;
// 不用系统键盘,自定义键盘
@property (readwrite, retain) UIView *inputView;
// 系统键盘和自定义键盘共存
@property (readwrite, retain) UIView *inputAccessoryView;
// 自动大写类型
@property(nonatomic) UITextAutocapitalizationType autocapitalizationType;
// 检查拼写是否正确
@property(nonatomic) UITextAutocorrectionType autocorrectionType;
// 修改键盘类型
@property(nonatomic) UIKeyboardType keyboardType;
// 修改返回类型
@property(nonatomic) UIReturnKeyType returnKeyType;
// 是否安全输入,比如用户输入密码
@property(nonatomic,getter=isSecureTextEntry) BOOL secureTextEntry;
一,作用:用户输入文字
二,常用属性
@property(nonatomic,copy) NSString *text;
// 设置文本内容颜色
@property(nonatomic,retain) UIColor *textColor;
// 设置字体
@property(nonatomic,retain) UIFont *font
// 对齐样式
@property(nonatomic) NSTextAlignment textAlignment;
// 设置风格,默认没有风格,需要设置
@property(nonatomic) UITextBorderStyle borderStyle;
// 提示用户输入内容文本
@property(nonatomic,copy) NSString *placeholder;
// 用户编辑时是否clear内容,默认为NO
@property(nonatomic) BOOL clearsOnBeginEditing;
// 自适应调整字体大小,默认为NO
@property(nonatomic) BOOL adjustsFontSizeToFitWidth;
三,常用属性
// 设置代理
@property(nonatomic,assign) id<UITextFieldDelegate> delegate;
// 设置背景,需要将textField实例的风格设置为None
@property(nonatomic,retain) UIImage *background;
// 设置textField不可用时的背景图片
@property(nonatomic,retain) UIImage *disabledBackground;
// 设置是否可编辑
@property(nonatomic,readonly,getter=isEditing) BOOL editing;
// 清除按钮的模式,默认不出现
@property(nonatomic) UITextFieldViewMode clearButtonMode;
// 自定义左视图
@property(nonatomic,retain) UIView *leftView;
// 自定义左视图出现的模式
@property(nonatomic) UITextFieldViewMode leftViewMode;
// 不用系统键盘,自定义键盘
@property (readwrite, retain) UIView *inputView;
// 系统键盘和自定义键盘共存
@property (readwrite, retain) UIView *inputAccessoryView;
// 自动大写类型
@property(nonatomic) UITextAutocapitalizationType autocapitalizationType;
// 检查拼写是否正确
@property(nonatomic) UITextAutocorrectionType autocorrectionType;
// 修改键盘类型
@property(nonatomic) UIKeyboardType keyboardType;
// 修改返回类型
@property(nonatomic) UIReturnKeyType returnKeyType;
// 是否安全输入,比如用户输入密码
@property(nonatomic,getter=isSecureTextEntry) BOOL secureTextEntry;
四,UITextFieldDemo
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
self.window.backgroundColor = [UIColor purpleColor];
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(90, 80, 140, 35);
[button addTarget:self action:@selector(test) forControlEvents:UIControlEventTouchUpInside];
[self.window addSubview:button];
UITextField *tf = [[UITextField alloc] initWithFrame:CGRectMake(60, 180, 200, 35)];
tf.tag = 101;
tf.delegate = self; // 设置代理
tf.textColor = [UIColor redColor];
tf.placeholder = @"用来提示用户";
tf.adjustsFontSizeToFitWidth = YES;
tf.clearsOnBeginEditing = YES;
tf.clearButtonMode = UITextFieldViewModeWhileEditing;
// tf.background = [UIImage imageNamed:@"navigation"];
tf.borderStyle = UITextBorderStyleRoundedRect;
[self.window addSubview:tf];
[tf release];
// 成为第一响应者
// [tf becomeFirstResponder];
// 自定义clear按钮
// UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];
// view.backgroundColor = [UIColor yellowColor];
// tf.rightView = view;
// [view release];
// tf.rightViewMode = UITextFieldViewModeUnlessEditing;
// 自定义系统键盘
// UIView *csView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 200)];
// csView.backgroundColor = [UIColor yellowColor];
// tf.inputView = csView;
// [csView release];
// 共用
// UIView *csView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
// csView.backgroundColor = [UIColor yellowColor];
// tf.inputAccessoryView = csView;
// [csView release];
// tf.secureTextEntry = YES;//设置密码
// tf.keyboardType = UIKeyboardTypeNumberPad;
// tf.returnKeyType = UIReturnKeyDone;
tf.autocapitalizationType = UITextAutocapitalizationTypeNone;
[self.window makeKeyAndVisible];
return YES;
}
- (void)test
{
UITextField *tf = (UITextField *)[self.window viewWithTag:101];
// 将键盘移除
[tf resignFirstResponder];
}