iOS:文本视图控件UITextView的详细使用
文本视图控件:UITextView
@property(nonatomic,assign) id<UITextViewDelegate> delegate; //代理
@property(nonatomic,copy) NSString *text; //文本
@property(nonatomic,retain) UIFont *font; //文本字体
@property(nonatomic,retain) UIColor *textColor; //文本颜色
@property(nonatomic) NSTextAlignment textAlignment; //文本对齐方式
@property(nonatomic) NSRange selectedRange; //选中的文本区域
@property(nonatomic,getter=isEditable) BOOL editable; //文本是否可以编辑
@property(nonatomic,getter=isSelectable) BOOL selectable ; //文本是否可选择
@property(nonatomic) UIDataDetectorTypes dataDetectorTypes; //检测文本中数字和链接
@property(nonatomic) BOOL allowsEditingTextAttributes; //是否允许编辑文本属性
@property(nonatomic,copy) NSAttributedString *attributedText ;//文本属性
@property(nonatomic,copy) NSDictionary *typingAttributes; //文本属性类型字典
@property (readwrite, retain) UIView *inputView; //输入视图
@property (readwrite, retain) UIView *inputAccessoryView; //输入域切换视图
@property(nonatomic,readonly) NSTextContainer *textContainer //放置文本的区域容器
@property(nonatomic, assign) UIEdgeInsets textContainerInset; //文本边距
@property(nonatomic,readonly) NSLayoutManager *layoutManager; //文本布局管理者
@property(nonatomic,readonly,retain) NSTextStorage *textStorage;//文本保存实例
@property(nonatomic, copy) NSDictionary *linkTextAttributes; //文本链接属性字典
方法:
※设置文本滚动区域
- (void)scrollRangeToVisible:(NSRange)range;
※ 初始化实例方法
- (instancetype)initWithFrame:(CGRect)frame textContainer:(NSTextContainer *)textContainer;
代理方法:
@protocol UITextViewDelegate <NSObject, UIScrollViewDelegate>
@optional
※点击进入文本将要开始编辑时触发的方法
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView;
※离开文本将要结束编辑时触发的方法
- (BOOL)textViewShouldEndEditing:(UITextView *)textView;
※文本已经开始编辑时触发的方法
- (void)textViewDidBeginEditing:(UITextView *)textView;
※文本已经结束编辑时触发的方法
- (void)textViewDidEndEditing:(UITextView *)textView;
※文本内容将要发生改变时触发的方法
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:
(NSString *)text;
※文本内容已经发生改变时触发的方法
- (void)textViewDidChange:(UITextView *)textView;
※文本内容已经选中时触发的方法
- (void)textViewDidChangeSelection:(UITextView *)textView;
※是否呼叫文本视图的链接
- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:
(NSRange)characterRange;
※是否呼叫文本视图依附
- (BOOL)textView:(UITextView *)textView shouldInteractWithTextAttachment:(NSTextAttachment *)textAttachment inRange:(NSRange)characterRange;
@end
通知:
UIKIT_EXTERN NSString * const UITextViewTextDidBeginEditingNotification; //文本开始编辑通知
UIKIT_EXTERN NSString * const UITextViewTextDidChangeNotification; //文本已经改变通知
UIKIT_EXTERN NSString * const UITextViewTextDidEndEditingNotification; //文本结束编辑通知
举例如下:
创建文本视图控件实例
// 创建文本视图控件实例
UITextView *textView = [[UITextView alloc]init];
设置文本视图的frame
//设置文本视图的frame
textView.frame = CGRectMake(, , , );
设置文本视图的文本内容
//设置文本视图的文本
textView.text = @"Copyright (c) 2015年 bjsxt. All rights reserved,文本视图控件UITextView,ViewController.m";
设置文本视图的字体大小
//设置文本字体
textView.font = [UIFont systemFontOfSize:];
设置文本可以选中
//设置文本可以选中
textView.selectable = YES;
设置文本可以编辑
//设置文本可以编辑
textView.editable = YES;
给文本视图加圆角边框
//给文本视图加圆角边框
textView.layer.borderColor = [[UIColor colorWithRed:230.0/255.0 green:250.0/255.0 blue:250.0/255.0 alpha:1.0]CGColor];
textView.layer.borderWidth = 3.0;
textView.layer.cornerRadius = 8.0f;
[textView.layer setMasksToBounds:YES];
设置代理
//设置代理
textView.delegate = self;
将控件添加到视图控制器的视图中
//添加控件到视图中
[self.view addSubview:textView];
以下是协议的方法:
#pragma mark -<UITextViewDelegate>
//※点击进入文本将要开始编辑时触发的方法
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView
{
NSLog(@"shouldBeginEditing");
return YES;
} //※离开文本将要结束编辑时触发的方法
- (BOOL)textViewShouldEndEditing:(UITextView *)textView
{
NSLog(@"ShouldEndEditing");
return YES;
} //※文本已经开始编辑时触发的方法
- (void)textViewDidBeginEditing:(UITextView *)textView
{
NSLog(@"DidBeginEditing");
} //※文本已经结束编辑时触发的方法
- (void)textViewDidEndEditing:(UITextView *)textView
{
NSLog(@"DidEndEditing");
} //※文本内容将要发生改变时触发的方法
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:
(NSString *)text
{
NSLog(@"shouldChangeTextInRange");
return YES;
} //※文本内容已经发生改变时触发的方法
- (void)textViewDidChange:(UITextView *)textView
{
NSLog(@"DidChange");
} //※文本内容已经选中时触发的方法
- (void)textViewDidChangeSelection:(UITextView *)textView
{
NSLog(@"DidChangeSelection");
}
演示截图如下:
内容被隐藏了一部分: 拖动滑动条查看隐藏的部分内容:

开始选择文本功能(select) 选中部分文本内容(edit)

添加内容(edit)

触发协议方法的执行结果如下:
当按钮点入文本视图时,触发的方法是:
-- ::41.624 文本视图控件UITextView[:] shouldBeginEditing
-- ::41.685 文本视图控件UITextView[:] DidBeginEditing
-- ::41.693 文本视图控件UITextView[:] DidChangeSelection
当编辑文本时(包括敲回车键)时,触发的方法是:
-- ::50.384 文本视图控件UITextView[:] shouldChangeTextInRange
-- ::50.386 文本视图控件UITextView[:] DidChangeSelection
-- ::50.386 文本视图控件UITextView[:] DidChange
当选中文本时,触发的方法是:
-- ::07.836 文本视图控件UITextView[:] DidChangeSelection
iOS:文本视图控件UITextView的详细使用的更多相关文章
- iOS:风火轮活动刷新视图控件UIActivityIndicatorView的详细使用
动态风火轮视图控件:UIActivityIndicatorView 介绍:它是一种类似于风火轮旋转的视图控件,可用作刷新数据时显示加载过程所用,继承自UIView. 类型: typedef N ...
- iOS:分段控件UISegmentedControl的详细使用
分段控件:UISegmentedControl 功能:分段的控制.页面的切换等. 介绍:当用户输入不仅仅是布尔值时,可使用分段控件(UISegmentedControl).分段控件提供一栏按钮 ...
- iOS:UITableView表格视图控件
UITableView:表格视图控件,继承滚动视图控件UIScrollView,(类似于UIPickerView选择器,它主要通过设置数据源代理和行为代理实现协议来设置单元格) 对表格的操作主要 ...
- iOS:网页视图控件UIWebView的详解
网页视图控件:UIWebView 功能:它是继承于UIView的,是一个内置的浏览器控件,以用来浏览从网络下载下来的网页或者本地上加载下来的文档. 枚举: //网页视图导航类型 typedef NS_ ...
- iOS:UIImageView图像视图控件
UIImageView:图像视图控件: 它是UIView的子类,因此也是视图控件,可以用来显示图像.因为它具有帧动画属性和操作方法,因此可以用来制作动画,其实动画就是很短的时间内,执行显示连续的 ...
- 从0到1搭建移动App功能自动化测试平台(2):操作iOS应用的控件
转自:http://debugtalk.com/post/build-app-automated-test-platform-from-0-to-1-Appium-interrogate-iOS-UI ...
- 无比迅速敏捷地开发iOS超精美控件
目录 前言 设计 编码 PaintCode 前言 自从人生第一篇博客<iOS中的预编译指令的初步探究>问世以来 浏览量竟然达到了360多,(路过的大神勿笑!)这些浏览量使我兴奋异常但又令我 ...
- iOS基础UI控件介绍-Swift版
iOS基础UI控件总结 iOS基础控件包括以下几类: 1.继承自NSObject:(暂列为控件) UIColor //颜色 UIImage //图像 2.继承自UIView: 只能相应手势UIGest ...
- iOS开发--UIKit控件之UISearchBar(搜索栏)
今天因为需求原因,需要用到搜索控件:之前一直没有用到过这个控件,所以去百度了一下,找到一篇可以说很齐全的资料,感谢这位作者. 然而,我并没有找到可以更改字体大小的属性或方法,希望有知道的告诉我一声,谢 ...
随机推荐
- Can't load standard profile: GRAY.pf
报错: java.lang.IllegalArgumentException: Can't load standard profile: GRAY.pf at java.awt.color.ICC_P ...
- git+jenkins在windows机器上新建一个slave节点【转载】
转至博客:上海-悠悠 前言 我们在跑自动化项目的时候,希望有单独的测试机能跑自动化项目,并且能集成到jenkins上构建任务.如果公司已经有jenkins环境了,那无需重新搭建. 只需在现有的平台基础 ...
- Linux 基础——ls 命令
第二天,继续学习Linux命令... 一.查看文件和目录列表的命令 ls:显示当前目录下的文件和目录,但是不会显示隐藏的文件和目录. ls -a:显示当前目录下的所有文件和目录. ls -l:显示当前 ...
- [水煮 ASP.NET Web API2 方法论](1-3)如何接收 HTML 表单请求
问题 我们想创建一个能够处理 HTML表单的 ASP.NET Web API 应用程序(使用 application/x-www-form-urlencoded 方式提交数据). 解决方案 我们可以创 ...
- http中使用json封装数据的性能测试
http中使用json封装数据的性能测试 一个项目使用json封装数据,接口例如: 客户端发送: POST /list.do HTTP/1.1 Host: zoomi.com.cn ...
- TCP握手协议简述
TCP握手协议简述在TCP/IP协议中,TCP协议提供可靠的连接服务,采用三次握手建立一个连接.第一次握手:建立连接时,客户端发送syn包(syn=j)到服务器,并进入SYN_SEND状态,等待服务器 ...
- 将字符串顺序重新排序DOM节点
对于一个已有的HTML结构: Haskell JavaScript Python Ruby Scheme <!-- HTML结构 --> <ol id="test-list ...
- python的ipython manage.py shell 引发的 No module named _argparse
环境是:Centos6.6 ,python 2.6 今晚,shell 中输入: # ipython manage.py shell 报错,说找不到命令: 我当时,觉得,我有可能没有安装ipython ...
- 【UVA 11077】 Find the Permutations (置换+第一类斯特林数)
Find the Permutations Sorting is one of the most used operations in real life, where Computer Scienc ...
- [BZOJ4709][JSOI2011]柠檬(斜率优化DP)
显然选出的每一段首尾都是相同的,于是直接斜率优化,给每个颜色的数开一个单调栈即可. #include<cstdio> #include<vector> #include< ...