1、基本用法:
UITextField * textField = [[UITextField alloc] initWithFrame:CGRectMake(30, 100, 200, 30)];
textField.backgroundColor = [UIColor whiteColor];
[self.view addSubview:textField];
textField.text = @"预置文字";
textField.borderStyle = UITextBorderStyleRoundedRect;//设置输入框的边框类型为圆角
效果:
2、UITextField的边框类型:
typedef NS_ENUM(NSInteger, UITextBorderStyle) {
UITextBorderStyleNone,
UITextBorderStyleLine,
UITextBorderStyleBezel,
UITextBorderStyleRoundedRect
};
上面1中是最后一种(圆角+阴影),下面看看其他三种
(1)、textField.borderStyle = UITextBorderStyleNone 无边框线
(2)、textField.borderStyle = UITextBorderStyleBezel 边框线 + 阴影 (效果可能不是很明显)
(3)、textField.borderStyle = UITextBorderStyleLine 仅有边框线
3、键盘的显示和隐藏
只要让textField变为第一响应者键盘就会显示,反之如果textField失去第一响应者,键盘就会隐藏。
@interface ViewController () <UITextFieldDelegate>
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.view.backgroundColor = [UIColor brownColor];
UITextField * textField = [[UITextField alloc] initWithFrame:CGRectMake(30, 100, 200, 30)];
textField.backgroundColor = [UIColor whiteColor];
[self.view addSubview:textField];
textField.text = @"预置文字";
textField.borderStyle = UITextBorderStyleRoundedRect;//设置输入框的边框类型为圆角
textField.delegate = self;
}
//点击键盘return键,让textField失去第一响应者,隐藏键盘,使用这个方法要设置代理UITextFieldDelegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return YES;
}
让textField成为第一响应者:
点击return使其失去第一响应者并隐藏键盘:
至于键盘类型,这里不再继续说,因为主要要说UITextField
4、文本的位置调整
首先将textField的高度调整为50,这样看着比较明显,然后设置textField的textAlignment属性
/* Values for NSTextAlignment */
typedef NS_ENUM(NSInteger, NSTextAlignment) {
NSTextAlignmentLeft = 0, // Visually left aligned
#if TARGET_OS_IPHONE
NSTextAlignmentCenter = 1, // Visually centered
NSTextAlignmentRight = 2, // Visually right aligned
#else /* !TARGET_OS_IPHONE */
NSTextAlignmentRight = 1, // Visually right aligned
NSTextAlignmentCenter = 2, // Visually centered
#endif
NSTextAlignmentJustified = 3, // Fully-justified. The last line in a paragraph is natural-aligned.
NSTextAlignmentNatural = 4, // Indicates the default alignment for script
} NS_ENUM_AVAILABLE_IOS(6_0);
什么都不设置,默认:
(1)、textField.textAlignment = NSTextAlignmentLeft
(2)、textField.textAlignment = NSTextAlignmentCenter
(3)、textField.textAlignment = NSTextAlignmentRight
(4)、textField.textAlignment = NSTextAlignmentJustified
(5)、textField.textAlignment = NSTextAlignmentNatural
5、文本的横向与纵向的调整
(1)、横向调整,横向调整可以参考4
(2)、纵向调整,改变UITextField的contentVerticalAlignment属性
typedef NS_ENUM(NSInteger, UIControlContentVerticalAlignment) {
UIControlContentVerticalAlignmentCenter = 0,
UIControlContentVerticalAlignmentTop = 1,
UIControlContentVerticalAlignmentBottom = 2,
UIControlContentVerticalAlignmentFill = 3,
};
textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter
textField.contentVerticalAlignment = UIControlContentVerticalAlignmentTop
textField.contentVerticalAlignment = UIControlContentVerticalAlignmentFill
6、设置输入框的字体及颜色
textField.font = [UIFont systemFontOfSize:30];
textField.textColor = [UIColor redColor];
7、提示信息
textField.placeholder = @"这是提示信息";
注意:如果需要设置提示信息,那就不能设置UITextField的text属性,否则提示信息不会显示,只会显示设置的text文本信息8、清空按钮
(1)、textField.clearButtonMode = UITextFieldViewModeNever (这个也是默认,不显示清空按钮)
(2)、textField.clearButtonMode = UITextFieldViewModeWhileEditing (编辑的时候显示,否则不显示)
(3)、textField.clearButtonMode = UITextFieldViewModeUnlessEditing (编辑时不显示,不编辑时显示)
编辑时,就是为第一响应者:
失去第一响应者:
(4)、textField.clearButtonMode = UITextFieldViewModeAlways (总是显示)
9、设置背景图片:
注意:只有把textField.borderStyle 设置为 UITextBorderStyleNone 的时候背景图片才会显示,切记
UIImage * image = [UIImage imageNamed:@"bg.png"];
UIImage * stretchImage = [image stretchableImageWithLeftCapWidth:30 topCapHeight:25];
textField.background = stretchImage;
这里如果同时backgroundColor,则backgroundColor失效
设置 disabledBackground
UIImage * image1 = [UIImage imageNamed:@"apple.jpg"];
UIImage * stretchImage1 = [image1 stretchableImageWithLeftCapWidth:30 topCapHeight:25];
textField.disabledBackground = stretchImage1;
textField.enabled = NO;//不可编辑,一定要设置这个
[self.view addSubview:textField];
这个背景图片拉伸后看着比较难看,见谅
10、追加UIView或者其子类
在文本输入框的左右侧可以追加任意的UIView及其子类,因为UITextField有leftView和rightView属性
UIImage * imageLeft = [UIImage imageNamed:@"dog.png"];
UIImage * imageRight = [UIImage imageNamed:@"apple.jpg"];
UIImageView * imageViewLeft = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
imageViewLeft.image = imageLeft;
UIImageView * imageViewRight = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
imageViewRight.image = imageRight;
textField.leftView = imageViewLeft;
textField.rightView = imageViewRight;
textField.leftViewMode = UITextFieldViewModeAlways;//这里跟清空按钮的模式是一样的
textField.rightViewMode = UITextFieldViewModeAlways;
把右边按钮
textField.rightViewMode = UITextFieldViewModeUnlessEditing;
11、文本输入框的状态监视
通过向UITextField的delegate属性设置为self,并把当前类设置为遵从UITextFieldDelegate协议,通过UITextFieldDelegate协议中的各种方法监视UITextField的各种状态
@protocol UITextFieldDelegate <NSObject>
@optional
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField; // return NO to disallow editing.
- (void)textFieldDidBeginEditing:(UITextField *)textField; // became first responder
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField; // return YES to allow editing to stop and to resign first responder status. NO to disallow the editing session to end
- (void)textFieldDidEndEditing:(UITextField *)textField; // may be called if forced even if shouldEndEditing returns NO (e.g. view removed from window) or endEditing:YES called
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string; // return NO to not change text
- (BOOL)textFieldShouldClear:(UITextField *)textField; // called when clear button pressed. return NO to ignore (no notifications)
- (BOOL)textFieldShouldReturn:(UITextField *)textField; // called when 'return' key pressed. return NO to ignore.
@end
@interface ViewController () <UITextFieldDelegate>
@end
textField.delegate = self;
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
//键盘上的return键被点击后调用
[textField resignFirstResponder];
return YES;
}
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
//编辑开始前被调用
//返回NO,不允许编辑,相当于textField.enabled = NO,返回YES允许编辑
return YES;
}
- (void)textFieldDidBeginEditing:(UITextField *)textField {
//文本框变为第一响应者之后调用
NSLog(@"我已经变为第一响应者了");
}
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField {
//编辑结束前被调用
//如果返回NO编辑将不会结束,一直处于编辑状态
return YES;
}
- (void)textFieldDidEndEditing:(UITextField *)textField {
NSLog(@"编辑结束后调用");
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
//文本修改前被调用
//range为修改范围,string为修改后的字符串,返回NO文本将不会被修改
NSLog(@"%@", NSStringFromRange(range));
NSLog(@"%@", string);
return YES;
}
- (BOOL)textFieldShouldClear:(UITextField *)textField {
//清除按钮被触摸的时候调用,返回YES输入框内容被清除,返回NO输入框内容不变
return YES;
}
12、自定义UITextField的时候返回新的各个区域的区域范围(自定义UITextField的时候,可以重写这些方法,来重置这些区域的区域范围)
- (CGRect)borderRectForBounds:(CGRect)bounds;
- (CGRect)textRectForBounds:(CGRect)bounds;
- (CGRect)placeholderRectForBounds:(CGRect)bounds;
- (CGRect)editingRectForBounds:(CGRect)bounds;
- (CGRect)clearButtonRectForBounds:(CGRect)bounds;
- (CGRect)leftViewRectForBounds:(CGRect)bounds;
- (CGRect)rightViewRectForBounds:(CGRect)bounds;