实现微信朋友圈动态列表
前言
項(xiàng)目中需要實(shí)現(xiàn)類似朋友圈動(dòng)態(tài)的列表,需要用到圖文混排,正好可以使用ibireme大神的開源控件YYText實(shí)現(xiàn)這種效果,在這里記錄一下使用過程中遇到的問題,參考文章iOS 保持界面流暢的技巧。
布局
列表中的主要布局如下圖所示,預(yù)先將每個(gè)cell的高度計(jì)算出來(lái),并且將高度緩存下來(lái),不重復(fù)計(jì)算高度。ibireme實(shí)現(xiàn)的微博列表Demo寫的已經(jīng)很清晰了,這里主要寫下自己在使用時(shí),遇到的一些問題。
1.TableView刷新時(shí)出現(xiàn)閃動(dòng)問題
TableView在刷新的時(shí)候,YYLabel會(huì)出現(xiàn)閃動(dòng),解決方法是將異步繪制關(guān)閉displaysAsynchronously = NO;,參考https://github.com/ibireme/YYKit/issues/64。
2.動(dòng)態(tài)Label默認(rèn)最多顯示六行
計(jì)算文本的行數(shù),YYTextContainer有最大行數(shù)maximumNumberOfRows屬性,根據(jù)這個(gè)屬性可以設(shè)置文本最大行數(shù)。
YYTextContainer *container = [YYTextContainer new];container.size = CGSizeMake(kWBCellContentWidth, HUGE);container.linePositionModifier = modifier;//計(jì)算文本的行數(shù),判斷顯示6行,還是顯示完全YYTextLayout *tmpTextLayout = [YYTextLayout layoutWithContainer:container text:text];NSUInteger rowCount = tmpTextLayout.rowCount; 復(fù)制代碼當(dāng)行數(shù)大于6行時(shí),顯示展開按鈕,點(diǎn)擊展開按鈕顯示全部文本,顯示收起按鈕,將當(dāng)前最大行數(shù)設(shè)置為6行。
//判斷是否隱藏展開按鈕if (rowCount > 6) {_isShowExpendButton = YES;//判斷展開或者收起狀態(tài)if (_isExpend == NO) {container.maximumNumberOfRows = 6;_expendButtonTitle = @"全文";}else{_expendButtonTitle = @"收起";}}else{_isShowExpendButton = NO;} 復(fù)制代碼3.匹配字符串
將動(dòng)態(tài)字符串中的[呵呵] ,[可愛]等字符串替換為表情圖片。將電話號(hào)碼,網(wǎng)址等進(jìn)行高亮顯示,并且為其設(shè)置userInfo,用于后面響應(yīng)用戶點(diǎn)擊事件。將這些方法放在了NSString+Emotion類中,方便調(diào)用。關(guān)于正則表達(dá)式怎么匹配,可以查看這篇文章從零開始學(xué)習(xí)正則表達(dá)式。
// 匹配 [表情]NSArray<NSTextCheckingResult *> *emoticonResults = [[EmoticonHelper regexEmoticon] matchesInString:text.string options:kNilOptions range:text.yy_rangeOfAll];NSUInteger emoClipLength = 0;for (NSTextCheckingResult *emo in emoticonResults) {if (emo.range.location == NSNotFound && emo.range.length <= 1) continue;NSRange range = emo.range;range.location -= emoClipLength;if ([text yy_attribute:YYTextHighlightAttributeName atIndex:range.location]) continue;if ([text yy_attribute:YYTextAttachmentAttributeName atIndex:range.location]) continue;NSString *emoString = [text.string substringWithRange:range];NSString *imagePath = [EmoticonHelper emoticonDic][emoString];UIImage *image = [EmoticonHelper imageWithPath:imagePath];if (!image) continue;NSAttributedString *emoText = [NSAttributedString yy_attachmentStringWithEmojiImage:image fontSize:font.pointSize];[text replaceCharactersInRange:range withAttributedString:emoText];//emoText的length都是1emoClipLength += range.length - 1;} 復(fù)制代碼響應(yīng)手機(jī)號(hào)和網(wǎng)址的點(diǎn)擊事件,獲取高亮狀態(tài)字符串的userInfo,然后進(jìn)行相應(yīng)的操作。
/// 點(diǎn)擊了 Label 的鏈接 - (void)cell:(TwitterCell *)cell didClickInLabel:(YYLabel *)label textRange:(NSRange)textRange{NSAttributedString *text = label.textLayout.text;if (textRange.location >= text.length) return;YYTextHighlight *highlight = [text yy_attribute:YYTextHighlightAttributeName atIndex:textRange.location];NSDictionary *info = highlight.userInfo;if (info.count == 0) return;if (info[kWBLinkURLName]) {[[UIApplication sharedApplication] openURL:[NSURL URLWithString:info[kWBLinkURLName]]];}else if (info[KWBLinkPhone]){NSMutableString *str2=[[NSMutableString alloc] initWithFormat:@"tel:%@",info[KWBLinkPhone]];UIWebView * callWebview = [[UIWebView alloc] init];[callWebview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:str2]]];[self.view addSubview:callWebview];} } 復(fù)制代碼4.評(píng)論列表點(diǎn)擊可以拷貝
評(píng)論視圖是用UITableView實(shí)現(xiàn)的,可以使用TableView的系統(tǒng)方法,實(shí)現(xiàn)長(zhǎng)按cell,彈出copy菜單,然后進(jìn)行復(fù)制[UIPasteboard generalPasteboard].string = model.comment。
- (BOOL)tableView:(UITableView *)tableView shouldShowMenuForRowAtIndexPath:(NSIndexPath *)indexPath {return YES; }- (BOOL)tableView:(UITableView *)tableView canPerformAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {if (action == @selector(copy:)) {return YES;}return NO; }- (void)tableView:(UITableView *)tableView performAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {if (action == @selector(copy:)) {TweetCommentModel *model;if (self.commentArray.count > 0) {model = self.commentArray[indexPath.row];}[UIPasteboard generalPasteboard].string = model.comment;} } 復(fù)制代碼YYKit中微博Demo的表情鍵盤,自己也照著作者的代碼敲了一篇,學(xué)到了很多。
最后代碼可以到動(dòng)態(tài)列表,表情鍵盤下載。
總結(jié)
以上是生活随笔為你收集整理的实现微信朋友圈动态列表的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: for循环效率测试
- 下一篇: 如何创建Android的菜单Menu