使用UITextView的dataDetectorTypes实现超链接需要注意的事项!
项目中需要在UITextView上识别URL,手机号码、邮箱地址等等信息。那么就用到了它的dataDetectorTypes属性。我的UITextView加在UITableViewCell上面的,当单元格多起来,重用的时候就发现文字的颜色出现了错乱问题,纠结了很久。之前单元格重用的时候就没有遇到过这种问题。仔细检查了一下,发现问题出在设置颜色和设置文字的顺序上面。
据我的理解:
UITextView设置了dataDetectorTypes,当赋值给它的text属性时会先用它的系统默认字体颜色处理普通文本和URL等信息(URL等是蓝色,其他是黑色),所以如果在给它的text赋值之前设置颜色相当于没用,因此要在设置为本之后再设置颜色,颜色重置。
下面是UITableViewCell的代码:
#import <UIKit/UIKit.h>
@interface HaveTextViewTableViewCell : UITableViewCell
{
UITextView *textView;
}
-(void)refreshWithRow:(NSInteger)row andText:(NSString *)text;
+(float)height;
@end
=====================.m文件:=======================================
#import "HaveTextViewTableViewCell.h"
@implementation HaveTextViewTableViewCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self)
{
textView = [[UITextView alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];
textView.editable = NO;
textView.dataDetectorTypes = UIDataDetectorTypeAll;
if([[[UIDevice currentDevice]systemVersion]floatValue]>=7.0)
{
textView.selectable = YES;//用法:决定UITextView 中文本是否可以相应用户的触摸,主要指:1、文本中URL是否可以被点击;2、UIMenuItem是否可以响应
}
textView.font = [UIFont systemFontOfSize:16];
[self.contentView addSubview:textView];
}
return self;
}
-(void)refreshWithRow:(NSInteger)row andText:(NSString *)text
{
//1.这种情况下会出现单元格重用的时候字体颜色不对应,而且长按都会出现UIActionSheet的问题!!!
// UITextView设置了dataDetectorTypes,当赋值给它的text时会先用系统默认字体颜色处理,在设置为本之后再设置颜色,颜色重置。
// UIColor *color = row%2==0?[UIColor redColor]:[UIColor blackColor];
// textView.textColor = color;
// textView.text = text;
//2.这种情况下不会出现上面的情况
textView.text = text;
UIColor *color = row%2==0?[UIColor redColor]:[UIColor blackColor];
textView.textColor = color;
}
+(float)height
{
return 50;
}
- (void)awakeFromNib {
// Initialization code
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
@end
=========UIViewController中的代码和UITableView的数据源============
dataArray = [NSMutableArray arrayWithCapacity:50];
for(NSInteger i=0;i<50;i++)
{
NSString *str = @"测试";
if(i%2==0)
{
str = [NSString stringWithFormat:@"%ld 测试 15021198368",i];
}
[dataArray addObject:str];
}
UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
tableView.dataSource = self;
tableView.delegate = self;
[self.view addSubview:tableView];
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return dataArray.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return [HaveTextViewTableViewCell height];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"cellIdentifier";
HaveTextViewTableViewCell *cell = (HaveTextViewTableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if(cell==nil)
{
cell = [[HaveTextViewTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
[cell refreshWithRow:indexPath.row andText:[dataArray objectAtIndex:indexPath.row]];
return cell;
}
这样当UITableView滑动几下,所有的文字颜色都变成了蓝色,都像是超链接了,长按都会出现UIActionSheet。
同步自网易博客 (查看原文)
使用UITextView的dataDetectorTypes实现超链接需要注意的事项!的更多相关文章
- ios 开发UI篇—UITextView
概述 UITextView可滚动的多行文本区域 UITextView支持使用自定义样式信息显示文本,并支持文本编辑.您通常使用文本视图来显示多行文本,例如在显示大型文本文档的正文时. UITextVi ...
- IOS 学习笔记(5) 控件 文本视图(UITextView)的使用方法
相对于UILabell所支持的较短文本内容,UITextView对于长文本的支持更好.UITextView能够以滚动的方式全部浏览到长文本,并且就像UILabel那样,从ISO6,他也提供了对NSAt ...
- 你真的了解UITextView吗?
一:首先查看一下关于UITextView的定义 NS_CLASS_AVAILABLE_IOS(2_0) @interface UITextView : UIScrollView <UITextI ...
- UITextView的使用详解
//初始化并定义大小 UITextView *textview = [[UITextView alloc] initWithFrame:CGRectMake(20, 10, 280, 30)]; te ...
- Swift - 多行文本输入框(UITextView)
1,多行文本控件的创建 1 2 3 4 let textview = UITextView(frame:CGRect(x:10, y:100, width:200, height:100)) text ...
- UITextView打开文字中的URL
1. 背景介绍 UITextView里显示的文字带有url,点击url可以打开对应的网页,可以分两种打开方式:(1)在App内打开url:(2)用safari打开url. 2. 实现代码: (1)声明 ...
- iOS - UITextView
前言 NS_CLASS_AVAILABLE_IOS(2_0) @interface UITextView : UIScrollView <UITextInput> @available(i ...
- TextKit学习(三)NSTextStorage,NSLayoutManager,NSTextContainer和UITextView
先上一张图: 这是使用UITextView时用到的iOS7新增加的类:NSTextContainer.NSLayoutManager.NSTextStorage及其相互关系: 这三个新出的类还没有在官 ...
- 【转】UITextView的使用详解
//初始化并定义大小 UITextView *textview = [[UITextView alloc] initWithFrame:CGRectMake(20, 10, 280, 30)]; te ...
随机推荐
- JasperReports报表数据源10
数据源的结构数据容器.同时生成报告,Jasper报表引擎获得来自数据源的数据.数据可以从数据库,XML文件,对象数组和集合中的对象来获得.我们将在本章填充报告所看到的fillReportXXX()方法 ...
- BZOJ2120&&2453 数颜色&&维护队列
2453: 维护队列 Time Limit: 10 Sec Memory Limit: 128 MB Submit: 1442 Solved: 678 [Submit][Status][Discuss ...
- consul原理
阅读目录 一.使用Consul做服务发现的若干姿势 1.https://www.cnblogs.com/bossma/p/9756809.html 阅读目录 启动第1个Server节点,集群要求要有3 ...
- CSS实例 display display 边距
CSS学习大纲 在标签上设置style属性: background-color:#2459a2 ; height:48px ; 编写CSS样式: 1.标签的style属性 2.写在head里面,sty ...
- [BZOJ2427][HAOI2010]软件安装-tarjan缩点-树上dp
<题面> 这个题真伤人 之前Tarjan和树规都没学好,吃了不少亏,仔仔细细的搞了一天,收获颇丰 先来一个Tarjan的链接:$\mathbb{O}$ 题目的数据比较友好: $dp$不对: ...
- ConcurrentDictionary让你的多线程代码更优美
ConcurrentDictionary是.net4.0推出的一套线程安全集合里的其中一个,和它一起被发行的还有ConcurrentStack,ConcurrentQueue等类型,它们的单线程版本( ...
- 通过Struts2Web应用框架深入理解MVC
Struts2是一个基于MVC设计模式的Web应用框架,它本质上相当于一个servlet. 一.用法简介: 1.Eclipse新建Dynamic Web Project, 项目名:Struts2Pro ...
- invalid use of null value
给mysql的数据表的一个字段插入数据,不成功, 然后在数据表设计中,把不是null勾选上,又提示 invalid use of null value 这种情况比较尴尬 只能删掉这一个字段,然后新建一 ...
- 我的第一个可用的Windows驱动完成了
看到了一些希望,就值得我继续执着下去. 虽然是很简单的一个小驱动,但是它包含了我学编程两年来的憧憬与努力... 在2011年5月份,我就想学驱动,但是多次的失败,让我很不耐烦,所以暂时搁置了.... ...
- java-Map-system
一 概述 0--星期日1--星期一... 有对应关系,对应关系的一方是有序的数字,可以将数字作为角标. public String getWeek(int num){ if(num<0 || n ...