標籤:
去年基於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:@"[email protected]譚中洞 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,下面是
不知道為什麼底部會添加一個著作權。我了個去~~ 隨便轉,不要看下面的,(但是也請註明出處).
著作權聲明:本文為博主原創文章,未經博主允許不得轉載。
IOS7 textkit 的相關