UITextView的基本用法
来源:互联网 发布:腾讯软件代理加盟 编辑:程序博客网 时间:2024/05/27 21:11
这里我们来了解UITextView的基本属性和代理方法
/*
继承 UIScrollView
和 UITextfield 属性基本相同
*/
首先声明属性
@property (nonatomic, strong) UITextView *textView;
// 初始化
self.textView = [[UITextView alloc] initWithFrame:CGRectMake(10, 50, CGRectGetWidth([UIScreen mainScreen].bounds) - 20, 36)];
// 文字
self.textView.text = @"请输入内容";
// 设置文字的颜色 字体
self.textView.textColor = [UIColor blackColor];
self.textView.font = [UIFont systemFontOfSize:20];
// 设置圆角
self.textView.layer.masksToBounds = YES;
self.textView.layer.cornerRadius = 5;
// 设置是否可以滑动
self.textView.scrollEnabled = YES; // 默认 YES
// 背景颜色为灰色
self.textView.backgroundColor = [UIColor grayColor];
[self.view addSubview:self.textView];
// 指定代理
self.textView.delegate = self;
下面我们来看一下UITextView的代理方法:记得一定要引入协议 <UITextViewDelegate>
// 开始编辑
- (void)textViewDidBeginEditing:(UITextView *)textView;
结束编辑
- (void)textViewDidEndEditing:(UITextView *)textView;
编辑发生改变
- (void)textViewDidChange:(UITextView *)textView;
焦点发生改变
- (void)textViewDidChangeSelection:(UITextView *)textView;
我们都知道UITextField有个属性就是placeholder 就是开始输入的时候的提示文字 但是UItextView没有 下面的方法就是来实现这个功能
// 开始编辑的时候
- (void)textViewDidBeginEditing:(UITextView *)textView {
判断 把self.textView的text置为 nil
if ([self.textView.text isEqualToString:@"请输入内容"]) {
self.textView.text = nil;
}
NSLog(@"开始编辑");
}
// 结束编辑
- (void)textViewDidEndEditing:(UITextView *)textView {
结束编辑的时候判断重新的附上值
if (self.textView.text.length < 1) {
self.textView.text = @"请输入内容";
}
NSLog(@"结束编辑");
}
// 这个方法实现了键盘的回收(但是前提是你不需要换行)你也可以加一个按钮来实现这一功能
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
if ([text isEqualToString:@"\n"]) {
[textView resignFirstResponder];
return NO;
}
return YES;
}
// textView的自适应高度
- (void)textViewDidChange:(UITextView *)textView{
// 计算文本的高度
CGSize constraintSize = CGSizeMake(textView.frame.size.width, 0);
NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:20]};
CGRect rect = [textView.text boundingRectWithSize:constraintSize options:NSStringDrawingUsesFontLeading|NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
// // 重新调整textView的高度
textView.frame = CGRectMake(textView.frame.origin.x,textView.frame.origin.y,textView.frame.size.width,rect.size.height + 12);
}
- UITextView的基本用法
- UITextView的基本用法
- UITextView的基本用法和代理方法
- UITextView 基本用法
- iOS关于UITextView的基本用法属性和协议
- iOS关于UITextView的基本用法属性和协议
- UITextView的用法
- UITextView 的简单用法
- UITextView控件的用法
- UITextView控件的用法详解
- UITextView控件的用法详解
- UITextView控件的用法详解
- UITextView控件的用法详解
- UITextView控件的用法详解
- UITextView控件的用法详解
- UITextView控件的用法详解
- UITextView控件的用法详解
- UITextView的用法及技巧
- C++程序Windows移植Linux注意事项
- iOS开发笔记--iOS应用架构谈 view层的组织和调用方案
- MySQL函数
- iOS 在xib或者storyboard中添加AutoLayout后,在代码中修改AutoLayout约束条件
- 第16回哼,我四岁就看动画!
- UITextView的基本用法
- 关于DB2与ORACLE相关常用函数比较分析
- iOS开发笔记--关于 @synchronized,这儿比你想知道的还要多
- linux命令行编辑快捷键
- Linux学习笔记(3)----文件系统的inode和block
- 自制安卓版iPhone计算器
- HDOJ 1085 Holding Bin-Laden Captive!(母函数)
- 一步一步学List Control控件的用法(第二步)----创建第一行
- Dynamics CRM 2016 Web API 消息列表