<原>DTCoreText学习(一)-DTAttributedTextCell原理

其实DTCoreText自带的cell就很好用了,解析html并且显示html都很方便,只要设置DTAttributedTextCell的

- (void)setHTMLString:(NSString *)html方法即可,其原理如下面所示

 1 - (id)initWithReuseIdentifier:(NSString *)reuseIdentifier accessoryType:(UITableViewCellAccessoryType)accessoryType
 2 {
 3     self = [super initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
 4     if (self) 
 5     {
 6         // don't know size jetzt because there's no string in it
 7         _attributedTextContextView = [[DTAttributedTextContentView alloc] initWithFrame:CGRectZero];
 8         _attributedTextContextView.edgeInsets = UIEdgeInsetsMake(5, 5, 5, 5);
 9         [self.contentView addSubview:_attributedTextContextView];
10     }
11     return self;
12 }

这是DTAttributedTextCell中定义的初始化方法  在cell的contentView上覆盖了一层DTAttributedTextContentView 而我们的html正是通过它显示出来的,

 1 - (void)setHTMLString:(NSString *)html
 2 {
 3     // we don't preserve the html but compare it's hash
 4     NSUInteger newHash = [html hash];
 5     
 6     if (newHash == _htmlHash)
 7     {
 8         return;
 9     }
10     
11     _htmlHash = newHash;
12     
13     NSData *data = [html dataUsingEncoding:NSUTF8StringEncoding];
14     NSAttributedString *string = [[NSAttributedString alloc] initWithHTML:data documentAttributes:NULL];
15     self.attributedString = string;
16 }
17 
18 - (void)setAttributedString:(NSAttributedString *)attributedString
19 {
20     if (_attributedString != attributedString)
21     {
22         _attributedString = attributedString;
23         
24         // passthrough
25         _attributedTextContextView.attributedString = _attributedString;
26     }
27 }

_attributedString的类型是NSAttributedString  我们把要显示的html传递给 setHTMLString:(NSString *)html   最后调用self.attributedString = string; 后进入setAttributedString:(NSAttributedString *)attributedString

最终设置  _attributedTextContextView.attributedString = _attributedString;达到显示的目的

DTAttributedTextCell常用的就这几个方法  一般了解这几个方法的使用  就能简单的实现利用 DTAttributedTextCell解析并显示html

转载于:https://www.cnblogs.com/bucengyongyou/archive/2012/09/02/2667521.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值