// // YMMonentsLayoutConfig.m // YUMI // // Created by YUMI on 2022/5/12. // #import "XPMomentsLayoutConfig.h" #import "DJDKMIMOMColor.h" ///Model #import "MomentsInfoModel.h" #import "MonentsCommentModel.h" #import "QEmotionHelper.h" @implementation XPMomentsLayoutConfig #pragma mark - 审核的布局 + (void)getNewLayoutMomentsModelWithDynamic:(MomentsInfoModel *)moments { CGFloat rowHeight = 10; ///用户信息和文字之间的间隙 rowHeight += kMOMENTS_CONTENT_SPACAE_HEIGHT; ///文字内容的高度 rowHeight += [self momentsContentHeight:moments]; ///图片的高度 rowHeight += [self getNewMomentsPicHeight:moments]; ///如果没有图片的话 间隙就只有一个 if (moments.type == MomentsContentType_Picture && moments.content.length > 0) { rowHeight += (kMOMENTS_CONTENT_SPACAE_HEIGHT); } ///话题的高度 if (moments.worldId > 0) { rowHeight += kMOMENTS_TEXT_TOPIC_HEIGHT; } ///底部操作栏的高度 rowHeight += kMOMENTS_TOOL_BAR_HEIGHT; moments.rowHeight = rowHeight; } + (CGFloat)getNewMomentsPicHeight:(MomentsInfoModel *)moments { ///计算图片的高度 NSInteger picCount = moments.dynamicResList.count; CGFloat picHeight = 0; CGFloat width = KScreenWidth-27*2; if (picCount == 0) { picHeight = 0; } else if(picCount == 1) { picHeight = width; } else if(picCount == 2) { picHeight = (width - kMOMENTS_PIC_SPACE) / 2; } else { if (picCount > 9) { picCount = 9; } CGFloat itemWidth = (width - kMOMENTS_PIC_SPACE * 2) / 3; NSInteger page = picCount % 3; NSInteger line = picCount / 3; if (page == 0) { picHeight = itemWidth * line + (line -1)* kMOMENTS_PIC_SPACE; } else { picHeight = itemWidth * (line +1) + line * kMOMENTS_PIC_SPACE; } } moments.picHeight = picHeight; return picHeight; } + (void)layoutMonentsModelWithDynamic:(MomentsInfoModel *)moments { CGFloat rowHeight = 10; ///用户信息和文字之间的间隙 rowHeight += kMOMENTS_CONTENT_SPACAE_HEIGHT; ///文字内容的高度 rowHeight += [self momentsContentHeight:moments]; ///图片的高度 rowHeight += [self momentsPicHeight:moments]; ///如果没有图片的话 间隙就只有一个 if (moments.type == MomentsContentType_Picture && moments.content.length > 0) { rowHeight += (kMOMENTS_CONTENT_SPACAE_HEIGHT); } ///话题的高度 if (moments.worldId > 0) { rowHeight += kMOMENTS_TEXT_TOPIC_HEIGHT; } ///底部操作栏的高度 rowHeight += kMOMENTS_TOOL_BAR_HEIGHT; moments.rowHeight = rowHeight; } + (void)layoutSimpleMomentsModel:(MomentsInfoModel *)moments { CGFloat rowHeight = kMOMENTS_CONTENT_SPACAE_HEIGHT; ///文字内容的高度 rowHeight += [self simpleMomentsContentHeight:moments]; ///图片的高度 rowHeight += [self momentsPicHeight:moments]; ///如果没有图片的话 间隙就只有一个 if (moments.type == MomentsContentType_Picture && moments.content.length > 0) { rowHeight += (kMOMENTS_CONTENT_SPACAE_HEIGHT * 2); }else { rowHeight += kMOMENTS_CONTENT_SPACAE_HEIGHT; } ///话题的高度 rowHeight += kMOMENTS_TEXT_TOPIC_HEIGHT; moments.rowHeight = rowHeight; } + (CGFloat)simpleMomentsContentHeight:(MomentsInfoModel *)moments { if (moments.content.length <= 0) { moments.contentHeight = 0; return 0; } ///计算文本的高度 if (!moments.contentAttribute) { NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] init]; if (moments.squareTop ) {//动态/广场 NSTextAttachment * attachment = [[NSTextAttachment alloc] init]; attachment.bounds = CGRectMake(0, 0, 25 * 1.3 ,10 * 1.3); attachment.image = [UIImage getLanguageImage:@"monents_info_top"]; NSAttributedString * starAttribute = [NSMutableAttributedString attributedStringWithAttachment:(NSTextAttachment *)attachment]; [attributedString insertAttributedString:starAttribute atIndex:0]; } [attributedString appendAttributedString:[self creatStrAttrByStr:moments.content attributed:@{NSFontAttributeName : [UIFont systemFontOfSize:15]}]]; attributedString.yy_lineSpacing = 5; attributedString.yy_lineBreakMode = NSLineBreakByWordWrapping; moments.contentAttribute = attributedString; } YYTextContainer *container = [YYTextContainer new]; container.size = CGSizeMake(kSIMPLE_MOMENTS_CONTENT_MAX_WIDTH, CGFLOAT_MAX); container.maximumNumberOfRows = 0; YYTextLayout *layout = [YYTextLayout layoutWithContainer:container text:moments.contentAttribute]; moments.numberOfText = layout.rowCount; CGFloat foldHeight = 0; if (layout.rowCount > 6) { foldHeight = kMOMENTS_FOLD_HEIGHT; NSInteger numberOfLines = moments.isFold ? 6 : 0; container.maximumNumberOfRows = numberOfLines; } YYTextLayout *realLayout = [YYTextLayout layoutWithContainer:container text:moments.contentAttribute]; CGFloat contentHeight = realLayout.textBoundingSize.height; moments.contentHeight =contentHeight; return contentHeight + foldHeight; } + (CGFloat)simpleMonentsPicHeight:(MomentsInfoModel *)moments { ///计算图片的高度 NSInteger picCount = moments.dynamicResList.count; CGFloat picHeight = 0; if (picCount == 0) { picHeight = 0; } else if(picCount == 1) { picHeight = kMOMENTS_PIC_ONE_WIDTH; } else if(picCount == 2) { picHeight = (kSIMPLE_MOMENTS_CONTENT_MAX_WIDTH - kMOMENTS_PIC_SPACE) / 2; } else { if (picCount > 9) { picCount = 9; } CGFloat itemWidth = (kSIMPLE_MOMENTS_CONTENT_MAX_WIDTH - kMOMENTS_PIC_SPACE * 2) / 3; NSInteger page = picCount % 3; NSInteger line = picCount / 3; if (page == 0) { picHeight = itemWidth * line + (line -1)* kMOMENTS_PIC_SPACE; } else { picHeight = itemWidth * (line +1) + line * kMOMENTS_PIC_SPACE; } } moments.picHeight = picHeight; return picHeight; } #pragma mark - 正常的布局 + (void)layoutMomentsModel:(MomentsInfoModel *)moments { CGFloat rowHeight = kMOMENTS_USER_INFO_HEIGHT; ///用户信息和文字之间的间隙 rowHeight += kMOMENTS_CONTENT_SPACAE_HEIGHT; ///文字内容的高度 rowHeight += [self momentsContentHeight:moments]; ///图片的高度 rowHeight += [self momentsPicHeight:moments]; ///如果没有图片的话 间隙就只有一个 if (moments.type == MomentsContentType_Picture && moments.content.length > 0) { rowHeight += (kMOMENTS_CONTENT_SPACAE_HEIGHT * 2); }else { rowHeight += kMOMENTS_CONTENT_SPACAE_HEIGHT; } ///话题的高度 rowHeight += kMOMENTS_TEXT_TOPIC_HEIGHT; ///底部操作栏的高度 rowHeight += kMOMENTS_TOOL_BAR_HEIGHT; moments.rowHeight = rowHeight; } + (CGFloat)momentsPicHeight:(MomentsInfoModel *)moments { ///计算图片的高度 NSInteger picCount = moments.dynamicResList.count; CGFloat picHeight = 0; if (picCount == 0) { picHeight = 0; } else if(picCount == 1) { picHeight = kMOMENTS_PIC_ONE_WIDTH; } else if(picCount == 2) { picHeight = (kMONENTS_CONTENT_MAX_WIDTH - kMOMENTS_PIC_SPACE) / 2; } else { if (picCount > 9) { picCount = 9; } CGFloat itemWidth = (kMONENTS_CONTENT_MAX_WIDTH - kMOMENTS_PIC_SPACE * 2) / 3; NSInteger page = picCount % 3; NSInteger line = picCount / 3; if (page == 0) { picHeight = itemWidth * line + (line -1)* kMOMENTS_PIC_SPACE; } else { picHeight = itemWidth * (line +1) + line * kMOMENTS_PIC_SPACE; } } moments.picHeight = picHeight; return picHeight; } + (CGFloat)momentsContentHeight:(MomentsInfoModel *)moments { if (moments.content.length <= 0) { moments.contentHeight = 0; return 0; } ///计算文本的高度 if (!moments.contentAttribute) { NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] init]; if (moments.squareTop ) {//动态/广场 NSTextAttachment * attachment = [[NSTextAttachment alloc] init]; attachment.bounds = CGRectMake(0, 0, 25 * 1.3 ,10 * 1.3); attachment.image = [UIImage getLanguageImage:@"monents_info_top"]; NSAttributedString * starAttribute = [NSMutableAttributedString attributedStringWithAttachment:(NSTextAttachment *)attachment]; [attributedString insertAttributedString:starAttribute atIndex:0]; } [attributedString appendAttributedString:[self creatStrAttrByStr:moments.content attributed:@{NSFontAttributeName : [UIFont systemFontOfSize:15]}]]; attributedString.yy_lineSpacing = 5; attributedString.yy_lineBreakMode = NSLineBreakByWordWrapping; moments.contentAttribute = attributedString; } YYTextContainer *container = [YYTextContainer new]; container.size = CGSizeMake(kMONENTS_CONTENT_MAX_WIDTH, CGFLOAT_MAX); container.maximumNumberOfRows = 0; YYTextLayout *layout = [YYTextLayout layoutWithContainer:container text:moments.contentAttribute]; moments.numberOfText = layout.rowCount; CGFloat foldHeight = 0; if (layout.rowCount > 6) { foldHeight = kMOMENTS_FOLD_HEIGHT; NSInteger numberOfLines = moments.isFold ? 6 : 0; container.maximumNumberOfRows = numberOfLines; } YYTextLayout *realLayout = [YYTextLayout layoutWithContainer:container text:moments.contentAttribute]; CGFloat contentHeight = realLayout.textBoundingSize.height; moments.contentHeight =contentHeight; return contentHeight + foldHeight; } + (NSMutableAttributedString *)creatStrAttrByStr:(NSString *)str attributed:(NSDictionary *)attribute{ if (str.length == 0 || !str) { str = @" "; } NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] initWithString:str attributes:attribute]; return attr; } ///计算评论的高度 + (CGFloat)commentCommentRowHeight:(MonentsCommentModel * )comment { if (comment.commentRowHeight > 0 && !comment.isReloadHeight) { return comment.commentRowHeight; } __block CGFloat rowHeight = 0; CGFloat commentTopHeight = 10 + 15 + 10;///10顶部的间隙 15 昵称的高度 ///内容 QEmotionHelper *faceManager = [QEmotionHelper sharedEmotionHelper]; NSMutableAttributedString * attribute = [faceManager attributedStringByText:comment.content font:[UIFont systemFontOfSize:15]]; YYTextContainer *container = [YYTextContainer new]; container.size = CGSizeMake(kMOMENTS_COMMENT_REPLY_MAX_WIDTH, CGFLOAT_MAX); container.maximumNumberOfRows = 0; YYTextLayout *realLayout = [YYTextLayout layoutWithContainer:container text:attribute]; CGFloat commentHeight = realLayout.textBoundingSize.height; commentHeight = MAX(commentHeight, 15); rowHeight += commentTopHeight; rowHeight += commentHeight; ///展开剩余的回复 if (comment.replyInfo.leftCount > 0) { rowHeight += 44; } if (comment.replyInfo.replyList.count > 0) { rowHeight += 10 * 2; } else { rowHeight += 10; } ///评论的高度 [comment.replyInfo.replyList enumerateObjectsUsingBlock:^(MonentsReplyModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { if (obj.content.length > 0) { [obj createContentAttribute]; CGFloat replayHeight = 0; CGFloat commentTopHeight = 10 + 15 + 10;///10顶部的间隙 15 昵称的高度 CGFloat commentBottomHeight = 10; YYTextContainer *container = [YYTextContainer new]; container.size = CGSizeMake(kMOMENTS_COMMENT_REPLY_MAX_WIDTH, CGFLOAT_MAX); container.maximumNumberOfRows = 0; YYTextLayout *realLayout = [YYTextLayout layoutWithContainer:container text:obj.contentAttribute]; CGFloat replyContentHeight = realLayout.textBoundingSize.height; replayHeight += commentTopHeight; replayHeight += replyContentHeight; replayHeight += commentBottomHeight; obj.replyRowHeight = replayHeight; rowHeight += replayHeight; } }]; comment.commentRowHeight = rowHeight; return rowHeight; } @end