去年基于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(@" lineFrag={%f,%f,%f,%f} ",lineFrag.origin.x,lineFrag.origin.y,lineFrag.size.width,lineFrag.size.height); NSLog(@" glyphPosition={%f,%f} ",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(@" range1 = %@ ",NSStringFromRange(range1)); NSRange range2 = [[string string] rangeOfString:@"#abcdef "]; [string addAttribute:NSLinkAttributeName value:@"URL://jsonstring" range:range2]; NSLog(@" range2 = %@ ",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,以下是效果图
不知道为什么底部会加入一个版权全部。我了个去~~ 随便转,不要看以下的,(可是也请注明出处).