项目中需要在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。

同步自网易博客 (查看原文)

最新文章

  1. 使用WordPress搭建自己的博客
  2. SpringMVC与Ajax交互
  3. infoq - neo4j graph db
  4. JS对象实现随机满天小星星实例
  5. [zz]Maya C++ API Programming Tips
  6. Ubuntu下移植OpenCv
  7. POJ 1390 Block
  8. 插入备份数据 报 IDENTITY_INSERT 为 ON 时解决方法
  9. [Selenium] 数字显示的月份转换为英文显示
  10. Bluetooth L2CAP介绍
  11. 基于gralde搭建spring boot项目
  12. selenium IDE--录制和回放脚本
  13. [Python] 应用kNN算法预测豆瓣电影用户的性别
  14. iOS UISearchDisplayController学习笔记
  15. Android Material Design带UI变化
  16. 自定义实现IEnumerable
  17. OpenCV 之 网络摄像头
  18. Android 之http编程
  19. 基于E-R模型的关系型数据库设计方法
  20. 【Caffe篇】--Caffe solver层从初始到应用

热门文章

  1. 微信网页授权demo2
  2. RDS for PostgreSQL 云盘加密功能使用方法
  3. AC自动机(模板) LUOGU P3808
  4. oracle建立job定期运行存储过程
  5. python 线性代数模块linalg
  6. 【CODEVS】2833 奇怪的梦境
  7. node的源码安装
  8. loj #10001. 「一本通 1.1 例 2」种树
  9. 【JZOJ3624】【SDOI2014】数数(count) AC自动机+数位dp
  10. 学习笔记(2)---Matlab 图像处理相关函数命令大全