您的位置:首页 > 产品设计 > UI/UE

iOS中 UITextView文本视图 技术分享

2015-10-19 20:53 465 查看
 UITextView:
 文本视图相比与UITextField直观的区别就是UITextView可以输入多行文字并且可以滚动显示浏览全文。
 UITextField的用处多,UITextView的用法也不少。常见UITextView使用在APP的软件简介、内容详情显示
 小说阅读显示、发表空间内容输入、说说文本框、评论文本框等。UITextView的使用有它本身的代理方法,也有
 继承于父类的方法。本身的方法有从开始编辑到结束编辑的整个过程的监听,继承的方法主要是继承于
 UIScrollView的方法,因为关于滚动的控制都属于UIScrollView的。根据常用经验,个人添加了在有导航栏
 的情况下可能输入文本框是下移的修复方法和添加文字时内容显示自动滚动到UITextView底部的实现方法。
#import "TextViewController.h"

@interface TextViewController ()<UITextViewDelegate>
@property(nonatomic,retain)UILabel *placeholderLabel;
@property(nonatomic,retain)UITextView *textView;
@end

@implementation TextViewController
- (void)dealloc
{
self.placeholderLabel = nil;
self.textView = nil;
[super dealloc];
}
- (void)viewDidLoad {
[super viewDidLoad];
//添加背景颜色
self.view.backgroundColor = [UIColor cyanColor];
//导航控制器名称
self.navigationController.title = @"UITextView的创建与使用";

//调用介绍TextView的相关属性
[self configureTextView];

}


介绍TextView的相关属性
- (void)configureTextView{

//创建TextView视图
self.textView = [[UITextView alloc]initWithFrame:CGRectMake(10, 80, self.view.frame.size.width - 80, 100)];
//添加到父视图
[self.view addSubview:self.textView];

//修复文本框的偏移量(下移)
self.automaticallyAdjustsScrollViewInsets = NO;
//设置UITextView的属性
//1.设置文本
//    self.textView.text = @"你好,我是小韩哥";
//2.设置文字的对齐方式
self.textView.textAlignment = NSTextAlignmentCenter;
//3.设置文字字体相关属性
self.textView.font = [UIFont systemFontOfSize:18];
//等等和UITextField几乎是一样的

//设置背景颜色
self.textView.backgroundColor = [UIColor grayColor];
//设置边框颜色和宽度
self.textView.layer.borderColor = [[UIColor colorWithRed:200.0/255 green:50/255 blue:10/255 alpha:1] CGColor];
self.textView.layer.borderWidth = 2;
//7.设置编辑属性,是否允许编辑(为NO时,只用来显示,依然可以使用选择和拷贝功能)
//    self.textView.editable = NO;
self.textView.editable = YES;

//模仿UITextField的placeholder属性
//     在textViewDidBeginEditing和textViewDidEndEditing内写实现方法。

self.placeholderLabel = [[UILabel alloc]initWithFrame:CGRectMake(5, 5, CGRectGetWidth(self.textView.frame), 20)];
//将UILabel的背景颜色设置为透明颜色clearColor
self.placeholderLabel.backgroundColor = [UIColor clearColor];
//设置UILabel的textColor属性为灰色grayColor
self.placeholderLabel.textColor = [UIColor grayColor];
//设置UILabel的text属性为需要的提示文字
self.placeholderLabel.text = @"请输入内容";
//设置UILabel的font属性和self.textView.font一致
self.placeholderLabel.font = self.textView.font;
//将UILabel添加到self.textView图层上
[self.textView addSubview:self.placeholderLabel];

//添加按钮及监听
UIButton *addButton = [UIButton buttonWithType:UIButtonTypeCustom];

addButton.frame = CGRectMake(CGRectGetMaxX(self.textView.frame)+10, 80, 50, 30);

addButton.backgroundColor = [UIColor lightGrayColor];

[addButton setTitle:@"添加" forState:UIControlStateNormal];

[addButton addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:addButton];

//添加代理协议
self.textView.delegate = self;
[self.placeholderLabel release];
[self.textView release];

}


#pragma mark- 按钮点击事件实现方法
- (void)btnClick:(UIButton*)sender{
NSLog(@"添加内容:欢迎来到韩俊强的CSDN博客");

self.textView.text = [self.textView.text stringByAppendingString:@"韩俊强的CSDN博客\n"];

//输入文字时自动滚动到底部
/*
1.拼接字符串赋值给self.textView.text;
2.计算NSRange自动滚动到底部。
NSRange是一个结构体,其中location是一个以0为开始的index,length是表示对象的长度。他们都是NSUInteger类型
*/
NSRange range = NSMakeRange([self.textView.text length]- 1, 1);
[self.textView scrollRangeToVisible:range];

//    [self.view endEditing:YES];
}


#pragma mark- 实现协议里的方法
//1、将要开始编辑
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView{

NSLog(@"将要开始编辑?");
return YES;
}
//2、将要完成编辑
- (BOOL)textViewShouldEndEditing:(UITextView *)textView{

NSLog(@"将要结束编辑?");
return YES;
}
//3、开始编辑
- (void)textViewDidBeginEditing:(UITextView *)textView{

NSLog(@"开始编辑。");
self.placeholderLabel.text = @"";
}
//4、完成编辑
- (void)textViewDidEndEditing:(UITextView *)textView{

NSLog(@"结束编辑。");

//模仿UTextField的placeholder属性
if (self.textView.text.length == 0) {
self.placeholderLabel.text = @"请输入内容";
}else{
self.placeholderLabel.text = @"";
}
}
//5、将要改变内容
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{

NSLog(@"将要改变内容?");

return YES;
}
//6、内容完成改变,只有在内容改变时才触发,而且这个改变内容是手动输入有效,用本例中得按钮增加内容不触发这个操作
- (void)textViewDidChange:(UITextView *)textView{

NSLog(@"改变内容。");
}
//7、内容被选中,几乎所有操作都会触发textViewDidChangeSelection,包括点击文本框、增加内容删除内容
- (void)textViewDidChangeSelection:(UITextView *)textView{

NSLog(@"选中内容。");
}

#pragma mark- 回收键盘
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{

//    [self.view endEditing:YES];
[self.textView resignFirstResponder];
}

最终效果:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: