去年基于5.0开发的时候,自己用coreText编写了一个富文本,所有的效果都实现的很好。但是没有去测试效率,不过在cell重用的时候表现不错,在4s上面也不会卡顿。
唯一一个问题就是,在使用AL的时候,不方便。所以,此次新版本是基于7.0开发。决定使用textkit重新编写一次。
下面是一些基本的使用:
@interface MMTextAttachment : NSTextAttachment
{
}
@end
@implementation MMTextAttachment
//I want my emoticon has the same size with line's height
- (CGRect)attachmentBoundsForTextContainer:(NSTextContainer *)textContainer proposedLineFragment:(CGRect)lineFrag glyphPosition:(CGPoint)position characterIndex:(NSUInteger)charIndex NS_AVAILABLE_IOS(7_0)
{
NSLog(@"\n lineFrag={%f,%f,%f,%f} \n",lineFrag.origin.x,lineFrag.origin.y,lineFrag.size.width,lineFrag.size.height);
NSLog(@"\n glyphPosition={%f,%f} \n",position.x,position.y);
CGFloat lineHeight = lineFrag.size.height;
CGFloat abc = 21.0;
return CGRectMake( 0 , (lineHeight - abc)/2.0 , abc , abc);
}
可以使用UITextView 或 UILabel,但是UITextView可以检测链接等,下面先看下UITextView
// 点击处理
// http://stackoverflow.com/questions/19332283/detecting-taps-on-attributed-text-in-a-uitextview-on-ios-7
NSMutableAttributedString * string = [[ NSMutableAttributedString alloc ] initWithString:@"1@谭中洞 2345678910 http://gutou.com 1112what happendwhatg一个地址 重庆市渝中区210号 02364646464 happendwhat happendwhat happendwhat happendwhat happend This is an example by @marcelofabri_ #abcdef " attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:14.0]} ] ;
MMTextAttachment * textAttachment = [[ MMTextAttachment alloc ] initWithData:nil ofType:nil ] ;
textAttachment.image = [UIImage imageNamed:@"002.png"] ;
NSAttributedString * textAttachmentString = [ NSAttributedString attributedStringWithAttachment:textAttachment ] ;
[string insertAttributedString:textAttachmentString atIndex:7] ;
MMTextAttachment * textAttachmen2t = [[ MMTextAttachment alloc ] initWithData:nil ofType:nil ] ;
[[SDWebImageManager sharedManager] downloadImageWithURL:[NSURL URLWithString:@"https://www.baidu.com/img/baidu_jgylogo3.gif?v=27357745.gif"] options:0 progress:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
// 使用网络下载,下载完成后,重新刷新下这个range
textAttachmen2t.image = image;
[_textView.layoutManager invalidateDisplayForCharacterRange:NSMakeRange(57, 1)]; // UILabel可以使用<span style="font-family: monospace; white-space: pre; background-color: rgb(240, 240, 240);">setNeedsDisplay
</span> }];
NSAttributedString * textAttachmentStr2ing = [ NSAttributedString attributedStringWithAttachment:textAttachmen2t ] ;
[string insertAttributedString:textAttachmentStr2ing atIndex:57] ;
//
NSRange range1 = [[string string] rangeOfString:@"@marcelofabri_"];
[string addAttribute:NSLinkAttributeName
value:@"URL://jsonstring"
range:range1];
NSLog(@"\n range1 = %@ \n",NSStringFromRange(range1));
NSRange range2 = [[string string] rangeOfString:@"#abcdef "];
[string addAttribute:NSLinkAttributeName
value:@"URL://jsonstring"
range:range2];
NSLog(@"\n range2 = %@ \n",NSStringFromRange(range2));
// [string addAttributes:@{NSForegroundColorAttributeName: [UIColor blueColor]} range:range1];
// [string addAttributes:@{NSForegroundColorAttributeName: [UIColor greenColor]} range:range2];
/*
// 自定link的颜色
NSDictionary *linkAttributes = @{
NSForegroundColorAttributeName: [UIColor blueColor],
NSUnderlineColorAttributeName: [UIColor redColor],
NSUnderlineStyleAttributeName: @(NSUnderlinePatternSolid)
};
_textView.linkTextAttributes = linkAttributes; // customizes the appearance of links
*/
_textView.dataDetectorTypes = UIDataDetectorTypeLink;
_textView.attributedText = string ;
_textView.selectable = YES; // 才能实现点击delegate
_textView.scrollEnabled = NO;// 设置为AL后才会自动算高
_textView.editable = NO;
_textView.delegate = self;
实现UITextViewDelegate
- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange
以上可以看到,网络下载,@等URL就都可以实现了。但是由于APPLE 在UITextView中把很多方法都实现了,所以导致,并不能很好的控制。比如URL检测,所有的URL都会被检测出来。另外@、#等为了表现为不同的颜色,也不好控制了。另一个,我不知道怎么解决的是:当点击链接的时候,就算是手指滑出链接范围,delegate还是会触发,这个不太好。还有一种判断点击,大家可以参考
http://stackoverflow.com/questions/19332283/detecting-taps-on-attributed-text-in-a-uitextview-on-ios-7
明天准备花一天时间,结合以前写过的重新编写一次。如果有啥后续再补充。
之前写过的解析有表情,@ ## 等可以点击的URL,下面是效果图
不知道为什么底部会添加一个版权所有。我了个去~~ 随便转,不要看下面的,(但是也请注明出处).