UITextView用法
来源:互联网 发布:centos 6.4更新 编辑:程序博客网 时间:2024/06/15 03:23
简介
UITextView控件用来显示多行并且可滚动的文字内容;它可以显示超出控件大小的内容;Iphone中的Note应用就是一个UITextView;
创建
在视图控制器中创建一个UITextView属性,然后在ViewDidLoad方法中实例化该控件,并设置文本内容、文字大小等属性;
#import "MyViewController.h" @interface MyViewController() @property(nonatomic, strong) UITextView *myTextView; @end @implementation -(void) viewDidLoad{ [super viewDidLoad]; self.myTextView = [[UITextView alloc] initWithFrame: self.view.bounds]; self.myTextView.text = @"please write some words here..."; self.myTextView.font = [UIFont systemFontOfSize : 16.0f]; self.contentInset = UIEdgeInsetMake(10.0f, 0.0f, 0.0f, 0.0f); [self.view addSubView: self.myTextView]; }
- 常见问题
在点击该控件编辑区域时,键盘会遮挡主底部一部分区域,使得用户无法看到这一部分的内容。可以通过监听以下事件来解决这个问题。
UIKeyboardWillShowNotification: 输入键盘即将从textField或者textView中出现时触发;
UIKeyboardDidShowNotification: 输入键盘处于显示状态时触发;
UIKeyboardWillHideNotification: 输入键盘即将消失时触发;
UIKeyboardDidHideNotification: 输入键盘完全消失时触发;
当键盘即将出现时,调整textView的位置。因此可以使用ContentInset属性来予以解决。
-(void) handleKeyboardIDidShow:(Notification *) notification{ NSValue *keyboardRectAsObject = [[notification userInfo] objectForKey: UIKeyboardFrameEndUserInfoKey]; CGRect keyboardRect = CGRectZero; [keyboardRectAsObject getVlaue: &keyboardRect]; //设置文本的下间距等于键盘高度 self.myTextView.contentInset = UIEdgeInsetMake(10.0f, 0.0f, keyboardRect.size.height, 0.0f);}-(void) handleKeyBoardWillHide:(Notification *) notification{ self.myTextView.contentInset =UIEdgeInsetMake(10.0f, 0.0f, 0.0f, 0.0f);}//在该方法中开始监听键盘事件-(void) viewWillAppear:(BOOL) paramAnimated{ [super viewWillAppear: paramAnimated]; [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector:(handleKeyboardDidShow:)] name: UIKeyboardDidShowNotification object: nil]; [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(handleKeyboardWillHide:) name: UIKeyboardWillHideNotification object: nil]; self.myTextView = [[UITextView alloc] initWithFrame: self.view.bounds]; self.myTextView.text = @"please write some words here... "; self.myTextView.font = [UIFont systemFontOfSize : 16.0f]; [self.view addSubView: self.myTextView];}//在该方法中停止监听键盘事件,防止视图控制器在后台监听键盘事件-(void) viewWillDisappear: (BOOL) paramAnimated{ [super viewWillDisappear]; [[NSNotificationCenter defaultCenter] removeObserver: self];}
0 0
- UITextView用法
- UITextView用法
- UITextView高级用法
- UITextView的用法
- UITextView高级用法
- UITextView用法总结
- UITextView的基本用法
- UITextView 用法小结
- UITextView 的简单用法
- UITextView的基本用法
- UITextView控件的用法
- UITextView用法总结
- UITextView 基本用法
- UITextView控件的用法详解
- UITextView控件的用法详解
- UITextView控件的用法详解
- UITextView控件的用法详解
- UITextView控件的用法详解
- PNG8和PNG24的区别
- lua解析赋值类型代码的过程
- 自己收集备注的一些资源
- 深入浅出SQL Server 2008 分区函数和分区表
- 2.属性详解(@property/@dynamic/@synthesize)
- UITextView用法
- ORM 框架之 greenDAO 使用心得
- PAT_乙级1006
- 【HNOI2012】【BZOJ2729】排队
- python中的Tkinter
- Linux下postgresql安装
- 2016寒假个人赛(1)B(数学)
- UIScrollView的属性总结
- spring-web源码解析之MappingMediaTypeFileExtensionResolver