• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
Bo.Ke
博客园    首页    新随笔    联系   管理    订阅  订阅
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];

 

 

 

 

posted on 2015-12-11 14:46  Bo.Ke  阅读(564)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3