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(搜索栏)
今天因为需求原因,需要用到搜索控件:之前一直没有用到过这个控件,所以去百度了一下,找到一篇可以说很齐全的资料,感谢这位作者. 然而,我并没有找到可以更改字体大小的属性或方法,希望有知道的告诉我一声,谢 ...
随机推荐
- mycncart自定义主题
本文是自己通过其他主题,自学的,如果有什么问题,可以提出建议? 参考资料:opencart官网 www.opencart.com 或 mycncart的官网上的一些教程 www.mycncart.c ...
- LCT 文档
file:///C:/Users/Frank/Downloads/QTREE%E8%A7%A3%E6%B3%95%E7%9A%84%E4%B8%80%E4%BA%9B%E7%A0%94%E7%A9%B ...
- 机器学习方法(八):随机采样方法整理(MCMC、Gibbs Sampling等)
转载请注明出处:Bin的专栏,http://blog.csdn.net/xbinworld 本文是对参考资料中多篇关于sampling的内容进行总结+搬运,方便以后自己翻阅.其实参考资料中的资料写的比 ...
- Nginx+PHP “No input file specified”错误的解决办法
配置官网商城php网站时候,界面报错“No input file specified” 原理: 任何对.php文件的请求,都简单地交给php-cgi去处理,但没有验证该php文件是否存在. PHP文件 ...
- vue-music 关于Player (播放器组件)--播放模式
播放器播放模式有三种,顺序播放,单曲循环,随机播放,定义在vuex 中的字段为 mode.点击切换播放模式的图标.切换模式判断是否为随机播放,如果是随机播放模式,则取得sequenceList 列表数 ...
- [lampp] 不能通过互联网连接数据库 MySQL is not accessable via network
LAMPP安装目录下的/etc/my.cnf文件注释掉skip-networking #skip-networking#skip-networking
- android ListView 在初始化时多次调用getView()原因分析
今天在做一个功能:在初始化ListView时,把第一行背景置为黄色,同时保存第一行对象,用于在点击其他行时将该行重新置为白色. if(position==0){ convertView.setBack ...
- java之异常
package com.text.exception; class Test{ void add(int a,int b) throws Exception { int c; c=a/b; Syste ...
- python之并发编程(线程\进程\协程)
一.进程和线程 1.进程 假如有两个程序A和B,程序A在执行到一半的过程中,需要读取大量的数据输入(I/O操作),而此时CPU只能静静地等待任务A读取完数据才能继续执行,这样就白白浪费了CPU资源.是 ...
- 如何将hdf5文件转换成tflite文件
我们用keras训练模型后,通常保存的模型格式类型为hdf5格式,也就是.h5文件. 但如果我们想要移植到移动端,特别是基于tensorflow支持的移动端,那就需要转换成tflite格式. 如何转换 ...