ios基础篇(六)——UITextView的常用方法及技巧

上篇说到了UITextField,我们先来说说UITextView和UITextField的不同:

UITextView支持多行输入; UITextFiled只支持单行;

UITextView没有placeholder属性; UITextField有placeholder属性

UITextview继承自UIScrollerView; UITextField继承自UIView

下面来说UITextField的使用及技巧:

一、新建一个textView
   //初始化
    textView = [[UITextView alloc] initWithFrame:(CGRect){10,50,200,100}];
    //背景颜色
    textView.backgroundColor = [UIColor yellowColor];
    //圆角
    textView.layer.cornerRadius = 3;
    textView.layer.masksToBounds = YES;
    //字体大小
    textView.font = [UIFont systemFontOfSize:20];
    //对齐方式
    textView.textAlignment = NSTextAlignmentLeft;
    //设置代理,需要在interface中声明UITextViewDelegate
    textView.delegate = self;
    //添加滚动区域
    textView.contentInset = UIEdgeInsetsMake(0, 0, -10, -10);
    //是否可以滑动
    textView.scrollEnabled =YES;
    //文本
    textView.text = @"UITextView";
    //加入视图
    [self.view addSubview:textView];

如图:

给TextView加一个有色边框,并设置背景图片

1 textView.layer.borderWidth = 5;
2 textView.layer.borderColor = [UIColor colorWithRed:0.2 green:0.4 blue:0.8 alpha:1];
3 //给图层添加背景图片
4 textView.layer.contents = [UIImage imageNamed:@"pic"].CGImage; 
如图:

二、键盘操作
1 //返回键的类型
2 textView.returnKeyType = UIReturnKeyDefault;
3  
4 //键盘类型
5 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里不用回车键,可以把回车键当做退出键盘的响应键。

 1 -(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
 2 {    
 3 
 4     if ([text isEqualToString:@"\n"]) {    
 5 
 6         [textView resignFirstResponder];    
 7 
 8         return NO;    
 9 
10     }
11 
12     return YES;    
13 
14 }

(3)还有你也可以自定义其他视图控件加载到键盘上用来退出,比如在弹出的键盘上面加一个view来放置退出键盘的Done按钮。

//定义一个toolBar
UIToolbar * topView = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 20)];
    
//设置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];

 

 

 

 

转载于:https://www.cnblogs.com/0320y/p/5038998.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值