上篇说到了UITextField,我们先来说说UITextView和UITextField的不同:
UITextView支持多行输入; UITextFiled只支持单行;
UITextView没有placeholder属性; UITextField有placeholder属性
UITextview继承自UIScrollerView; UITextField继承自UIView
下面来说UITextField的使用及技巧:
一、新建一个textView
//初始化
textView = [[UITextView alloc] initWithFrame:(CGRect){,,,}];
//背景颜色
textView.backgroundColor = [UIColor yellowColor];
//圆角
textView.layer.cornerRadius = ;
textView.layer.masksToBounds = YES;
//字体大小
textView.font = [UIFont systemFontOfSize:];
//对齐方式
textView.textAlignment = NSTextAlignmentLeft;
//设置代理,需要在interface中声明UITextViewDelegate
textView.delegate = self;
//添加滚动区域
textView.contentInset = UIEdgeInsetsMake(, , -, -);
//是否可以滑动
textView.scrollEnabled =YES;
//文本
textView.text = @"UITextView";
//加入视图
[self.view addSubview:textView];
如图:
给TextView加一个有色边框,并设置背景图片
textView.layer.borderWidth = ;
textView.layer.borderColor = [UIColor colorWithRed:0.2 green:0.4 blue:0.8 alpha:];
//给图层添加背景图片
textView.layer.contents = [UIImage imageNamed:@"pic"].CGImage;
如图:
二、键盘操作
//返回键的类型
textView.returnKeyType = UIReturnKeyDefault; //键盘类型
textView.keyboardType = UIKeyboardTypeDefault;
三、UITextView退出键盘的几种方式
(1)如果你程序是有导航条的,可以在导航条上面加多一个Done的按钮,用来退出键盘,当然要先实现UITextViewDelegate。
- (void)textViewDidBeginEditing:(UITextView *)textView { UIBarButtonItem *done = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(doneAction)]; self.navigationItem.rightBarButtonItem = done; } - (void)textViewDidEndEditing:(UITextView *)textView { self.navigationItem.rightBarButtonItem = nil; } - (void) doneAction { [textView resignFirstResponder]; }
(2)如果你的textvView里不用回车键,可以把回车键当做退出键盘的响应键。
-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{ if ([text isEqualToString:@"\n"]) { [textView resignFirstResponder]; return NO; } return YES; }
(3)还有你也可以自定义其他视图控件加载到键盘上用来退出,比如在弹出的键盘上面加一个view来放置退出键盘的Done按钮。
//定义一个toolBar
UIToolbar * topView = [[UIToolbar alloc]initWithFrame:CGRectMake(, , , )]; //设置style
[topView setBarStyle:UIBarStyleBlack]; //定义两个flexibleSpace的button,放在toolBar上,这样完成按钮就会在最右边
UIBarButtonItem * button1 =[[UIBarButtonItem alloc]initWithBarButtonSystemItem: UIBarButtonSystemItemFlexibleSpace target:self action:nil]; UIBarButtonItem * button2 = [[UIBarButtonItem alloc]initWithBarButtonSystemItem: UIBarButtonSystemItemFlexibleSpace target:self action:nil]; //定义完成按钮
UIBarButtonItem * doneButton = [[UIBarButtonItem alloc]initWithTitle:@"完成" style:UIBarButtonItemStyleDone target:self action:@selector(resignKeyboard)]; //在toolBar上加上这些按钮
NSArray * buttonsArray = [NSArray arrayWithObjects:button1,button2,doneButton,nil];
[topView setItems:buttonsArray]; [textView setInputAccessoryView:topView];
如图:
四、隐藏键盘
- (void)resignKeyboard {
[textView resignFirstResponder];
}
五、添加键盘的监听事件
//注册通知,监听键盘弹出事件
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow) name:UIKeyboardDidShowNotification object:nil]; //注册通知,监听键盘消失事件
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidHidden) name:UIKeyboardDidHideNotification object:nil];