使用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。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
UITextView是UIKit框架中的一个控件,可以用来显示和编辑长文本。而富文本则是指带有丰富样式的文本,可以设置文字的字体、颜色、大小、间距、行高等属性。 要在UITextView实现富文本,首先需要创建一个NSAttributedString对象,并通过NSMutableAttributedString来设置文字的样式。NSAttributedString是不可变的,而NSMutableAttributedString可以修改和添加样式。 创建NSMutableAttributedString对象后,可以使用其方法来设置文字的样式,比如设置字体可以使用NSFontAttributeName属性,设置颜色可以使用NSForegroundColorAttributeName属性,设置字号可以使用NSFontAttributeName属性,设置段落样式可以使用NSParagraphStyleAttributeName属性等等。 设置完成后,就可以将NSMutableAttributedString对象赋值给UITextView的attributedText属性,以实现富文本的显示。 例如,我们想将某个UITextView的文字样式设置为红色、字号为20、字体为粗体,可以按如下方式设置: ``` NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:@"这是富文本"]; [attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, attributedString.length)]; [attributedString addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:20] range:NSMakeRange(0, attributedString.length)]; textView.attributedText = attributedString; ``` 通过上述代码,就可以在UITextView中显示带有红色、字号为20、字体为粗体的文字。 除了以上示例外,UITextView还支持更多的富文本样式设置,根据具体需求,可以设置更多的属性来实现更丰富的文本效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值