项目中需要在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实现超链接需要注意的事项!的更多相关文章

  1. ios 开发UI篇—UITextView

    概述 UITextView可滚动的多行文本区域 UITextView支持使用自定义样式信息显示文本,并支持文本编辑.您通常使用文本视图来显示多行文本,例如在显示大型文本文档的正文时. UITextVi ...

  2. IOS 学习笔记(5) 控件 文本视图(UITextView)的使用方法

    相对于UILabell所支持的较短文本内容,UITextView对于长文本的支持更好.UITextView能够以滚动的方式全部浏览到长文本,并且就像UILabel那样,从ISO6,他也提供了对NSAt ...

  3. 你真的了解UITextView吗?

    一:首先查看一下关于UITextView的定义 NS_CLASS_AVAILABLE_IOS(2_0) @interface UITextView : UIScrollView <UITextI ...

  4. UITextView的使用详解

    //初始化并定义大小 UITextView *textview = [[UITextView alloc] initWithFrame:CGRectMake(20, 10, 280, 30)]; te ...

  5. Swift - 多行文本输入框(UITextView)

    1,多行文本控件的创建 1 2 3 4 let textview = UITextView(frame:CGRect(x:10, y:100, width:200, height:100)) text ...

  6. UITextView打开文字中的URL

    1. 背景介绍 UITextView里显示的文字带有url,点击url可以打开对应的网页,可以分两种打开方式:(1)在App内打开url:(2)用safari打开url. 2. 实现代码: (1)声明 ...

  7. iOS - UITextView

    前言 NS_CLASS_AVAILABLE_IOS(2_0) @interface UITextView : UIScrollView <UITextInput> @available(i ...

  8. TextKit学习(三)NSTextStorage,NSLayoutManager,NSTextContainer和UITextView

    先上一张图: 这是使用UITextView时用到的iOS7新增加的类:NSTextContainer.NSLayoutManager.NSTextStorage及其相互关系: 这三个新出的类还没有在官 ...

  9. 【转】UITextView的使用详解

    //初始化并定义大小 UITextView *textview = [[UITextView alloc] initWithFrame:CGRectMake(20, 10, 280, 30)]; te ...

随机推荐

  1. Thinkphp M方法出错,D方法却可以

    错误回顾: M('Local')->find(); //报错 //错误信息:Table 'test.local' doesn't exist [ SQL语句 ] : SHOW COLUMNS F ...

  2. Axure 工具的使用

    Axure RP是一款专业的快速原型设计工具,Axure RP是美国Axure Software Solution公司旗舰产品,是一个专业的快速原型设计工具,让负责定义需求和规格.设计功能和界面的专家 ...

  3. spring boot指定外部配置的坑

    外部配置文件所在目录path/to/dir 指定--spring.config.location=path/to/dir 项目启动,没有使用任何配置文件,项目外和jar包中的都没有使用 这是因为其把p ...

  4. myeclipse10.7的破解 不需要去CSDN付费下载-免csdn费下载

    吐槽一下,大票CSDN博主,在博文里基本不放干货,都弄成附件,放在csdn付费下载,一个破解办法,竟然50元,好在我是vip用户,不在乎价格,特此 这篇文章搬运一下资源给大家免费下载 顺便纠正一下其文 ...

  5. springMVC原理解析

    1:SpringMVC运行原理 2:工作流程 (1)客户端(浏览器)发送请求,直接请求到DispatcherServlet. (2)DispatcherServlet根据请求信息调用HandlerMa ...

  6. Eviews9.0---软件安装

    EViews是Econometrics Views的缩写,直译为计量经济学观察,通常称为计量经济学软件包.它的本意是对社会经济关系与经济活动的数量规律,采用计量经济学方法与技术进行“观察”.计量经济学 ...

  7. linux挂载点 和 文件系统$ mount$ cat /etc/fstab$ vgs$ pvs$ lvs$ df -h$ lsof +D / /* beware not to kill your box */

    $ mount$ cat /etc/fstab$ vgs$ pvs$ lvs$ df -h$ lsof +D / /* beware not to kill your box */ 一共挂载了多少文件 ...

  8. Leetcode56. Merge Intervals合并区间

    给出一个区间的集合,请合并所有重叠的区间. 示例 1: 输入: [[1,3],[2,6],[8,10],[15,18]] 输出: [[1,6],[8,10],[15,18]] 解释: 区间 [1,3] ...

  9. Leetcode24.Swap Nodes in Pairs两两交换链表中的节点

    给定一个链表,两两交换其中相邻的节点,并返回交换后的链表. 示例: 给定 1->2->3->4, 你应该返回 2->1->4->3. 说明: 你的算法只能使用常数的 ...

  10. ubuntu安装搜狗输入法后无法使用goland的快捷键 ctrl+alt+B

    安装了搜狗拼音后,其快捷键ctrl+alt+b会启动软键盘,造成与其他编辑器快捷键的冲突. 为了禁止使用ctrl+alt+b启动软键盘,可以: 1. 在搜狗拼音输入法选择设置 2. 高级设置 3. 高 ...