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(搜索栏)
今天因为需求原因,需要用到搜索控件:之前一直没有用到过这个控件,所以去百度了一下,找到一篇可以说很齐全的资料,感谢这位作者. 然而,我并没有找到可以更改字体大小的属性或方法,希望有知道的告诉我一声,谢 ...
随机推荐
- React Native - 4 ListView 简单使用
1. 首先要import ListView组件 2. 使用如下代码,注意ListView里的dataSource大小写,我当时把S给小写了,结果花了半个多小时找原因…… 3. 运行结果
- echarts画k线图
var charset = echarts.init(document.getElementById("k_line")) $.get(k_line.url_A).done(fun ...
- vue-music 关于playlist (底部播放列表组件)
建立playlist.vue 组件,在player.vue 组件中引用,点击迷你播放器的播放列表按钮由下至上弹出这个层,所以在player.vue 播放器组件中引用 在playlist.vue 组件中 ...
- python 字符集转换-灰常慢
代码 def toUni (text): str = text try: charstyle = chardet.detect(text) # print 'confidence: ', charst ...
- 初始pip
关于pip包括下面的东西还不是很懂,慢慢的了解,我的pip是从https://bootstrap.pypa.io/get-pip.py 粘贴并命名为 get-pip.py 后,执行 python ge ...
- CodeForces 143C Help Farmer
暴力枚举. 枚举最小的那个数字,不会超过$1000$,剩下的两个数字根号的效率枚举一下即可. #include<bits/stdc++.h> using namespace std; lo ...
- window10 Powershell使用curl命令报错解决方法
报错信息:curl : 无法分析响应内容,因为 Internet Explorer 引擎不可用,或者 Internet Explorer 的首次启动配置不完整.请指定 UseBasicParsing ...
- Codeforces Round #404 (Div. 2) C 二分查找
Codeforces Round #404 (Div. 2) 题意:对于 n and m (1 ≤ n, m ≤ 10^18) 找到 1) [n<= m] cout<<n; 2) ...
- [BZOJ5250][九省联考2018]秘密袭击(DP)
5250: [2018多省省队联测]秘密袭击 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 3 Solved: 0[Submit][Status][D ...
- AGC 026 C - String Coloring
题面在这里! 比较简单的折半搜索,推一下hash函数,要求正反最后相等就行了. #include<bits/stdc++.h> #define ll unsigned long long ...