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(搜索栏)
今天因为需求原因,需要用到搜索控件:之前一直没有用到过这个控件,所以去百度了一下,找到一篇可以说很齐全的资料,感谢这位作者. 然而,我并没有找到可以更改字体大小的属性或方法,希望有知道的告诉我一声,谢 ...
随机推荐
- SPOJ Two Paths
Description 给定一个无向图,含有一定的路.从中找出两个最长的路径(每条路径有一些相通路组成)这两个路径不能经过公共的点,求何时二路径的乘积最大. 本题给出的无向图是一棵树,每边权值为1. ...
- python import模块的搜索路径
当在py代码中import所依赖的模块时, python是从哪里找到这些模块呢,即模块的搜索路径是啥? 默认情况下,Python解释器会搜索当前目录.所有已安装的内置模块和第三方模块,搜索路径存放在s ...
- 走进 Cake for .NET
一.什么是 Cake Cake(C# Make) 是一个使用 C# DSL 面向 Task 的跨平台构建自动化系统,像编译代码,复制文件和文件夹,运行单元测试,压缩文件和构建 NuGet 包. 更多 ...
- php 正则验证字符串是否为数字
PHP 正则验证字符串是否为数字 方法一: php中利用正则表达式验证字符串是否为数字一件非常容易的事情,最主要的是如何写好正则表达式以及掌握正则表达式的写法,在此利用正则表达式的方式来列举一下判断数 ...
- python 正则表达式匹配中文(转)
网上的一篇文章,做了整理,作者已无从考证,谢谢了 s=""" en: Regular expression is a powerful tool for manipula ...
- Sqli-labs less 12
Less-12 本关和less11是类似的,只是在id 的参数的处理上有一定的不同 当输入username:admin" Password: (随便) 报错后的结果为: You have a ...
- Friends number NBUT - 1223 (暴力打表)
Paula and Tai are couple. There are many stories between them. The day Paula left by airplane, Tai s ...
- Codeforces Round #196 (Div. 1) 题解
(CF唯一不好的地方就是时差……不过还好没去考,考的话就等着滚回Div. 2了……) A - Quiz 裸的贪心,不过要用矩阵乘法优化或者直接推通式然后快速幂.不过本傻叉做的时候脑子一片混乱,导致WA ...
- [Codeforces #190] Tutorial
Link: Codeforces #190 传送门 A: 明显答案为$n+m-1$且能构造出来 #include <bits/stdc++.h> using namespace std; ...
- 2017 icpc 南宁网络赛
2000年台湾大专题...英语阅读输入输出专场..我只能说很强势.. M. Frequent Subsets Problem The frequent subset problem is define ...