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(搜索栏)
今天因为需求原因,需要用到搜索控件:之前一直没有用到过这个控件,所以去百度了一下,找到一篇可以说很齐全的资料,感谢这位作者. 然而,我并没有找到可以更改字体大小的属性或方法,希望有知道的告诉我一声,谢 ...
随机推荐
- 安装node的最新版本
前段时间小试了一下node 这段时间就差不多忘了 恩 然后现在自己想去回顾一下,然后流程想再好好弄一遍 争取掌握node 因为我现在已经安装了 一个node版本 那我想安装最新版本吧 首先,看看你的n ...
- pku 2777(经典线段树染色问题)
Count Color Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 41202 Accepted: 12458 Des ...
- windows网卡命令
netsh interface ip set address name="本地连接" source=dhcpnetsh interface ip set dns name=&quo ...
- Docker与Android Studio的冲突问题
在行业内,VisualBox.VMware.Hyper-V冲突也不是什么秘密了 我在公司的电脑上先安装了Docker,并在安装Docker之前在BIOS中开启了虚拟化支持,所以在启动Docker时没有 ...
- Aras Innovator 11 sp2 firefox客户端设置
在上一篇文章<Aras Innovator 11 sp2 安装>后,服务器算是安装好了,还需要在使用的客户端进行设置才可以正常使用Aras Innovator 该篇为firefox设置,还 ...
- 【招聘需求】前端开发/PHP工程师【往全栈发展】
1.熟悉html.css,了解javascript2.熟悉任何一种服务端编程语言(php.python.java.asp.jsp.c.c++.c#,go等等)3.热爱工作,接受加班者优先 我们是公司内 ...
- HDU 5127.Dogs' Candies-STL(vector)神奇的题,set过不了 (2014ACM/ICPC亚洲区广州站-重现赛(感谢华工和北大))
周六周末组队训练赛. Dogs' Candies Time Limit: 30000/30000 MS (Java/Others) Memory Limit: 512000/512000 K ( ...
- Apache配置文件相关命令
转:http://www.365mini.com/page/apache-options-directive.htm Options指令是Apache配置文件中一个比较常见也比较重要的指令,Optio ...
- Luogu P3178 树上操作(树链剖分+线段树)
题意 见原题 题解 重链剖分模板题 #include <cstdio> #include <algorithm> using std::swap; typedef long l ...
- Codeforces Round #300 Quasi Binary(DP)
Quasi Binary time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...