Files
yinmeng-ios/xplan-ios/Main/Monents/Presenter/XPMonentsLayoutConfig.m
2022-08-26 21:38:47 +08:00

161 lines
5.8 KiB
Objective-C

//
// XPMonentsLayoutConfig.m
// xplan-ios
//
// Created by 冯硕 on 2022/5/12.
//
#import "XPMonentsLayoutConfig.h"
#import <YYText/YYText.h>
#import "ThemeColor.h"
///Model
#import "MonentsInfoModel.h"
#import "MonentsCommentModel.h"
@implementation XPMonentsLayoutConfig
+ (void)layoutMonentsModel:(MonentsInfoModel *)monents {
CGFloat rowHeight = kMONENTS_USER_INFO_HEIGHT;
///用户信息和文字之间的间隙
rowHeight += kMONENTS_CONTENT_SPACAE_HEIGHT;
///文字内容的高度
rowHeight += [self monentsContentHeight:monents];
///图片的高度
rowHeight += [self monentsPicHeight:monents];
///如果没有图片的话 间隙就只有一个
if (monents.type == MonentsContentType_Picture) {
rowHeight += (kMONENTS_CONTENT_SPACAE_HEIGHT * 2);
}else {
rowHeight += kMONENTS_CONTENT_SPACAE_HEIGHT;
}
///话题的高度
rowHeight += kMONENTS_TEXT_TOPIC_HEIGHT;
///底部操作栏的高度
rowHeight += kMONENTS_TOOL_BAR_HEIGHT;
monents.rowHeight = rowHeight;
}
+ (CGFloat)monentsPicHeight:(MonentsInfoModel *)monents {
///计算图片的高度
NSInteger picCount = monents.dynamicResList.count;
CGFloat picHeight = 0;
if (picCount == 0) {
picHeight = 0;
} else if(picCount == 1) {
picHeight = kMONENTS_PIC_ONE_WIDTH;
} else if(picCount == 2) {
picHeight = (kMONENTS_CONTENT_MAX_WIDTH - kMONENTS_PIC_SPACE) / 2;
} else {
if (picCount > 9) {
picCount = 9;
}
CGFloat itemWidth = (kMONENTS_CONTENT_MAX_WIDTH - kMONENTS_PIC_SPACE * 2) / 3;
NSInteger page = picCount % 3;
NSInteger line = picCount / 3;
if (page == 0) {
picHeight = itemWidth * line + (line -1)* kMONENTS_PIC_SPACE;
} else {
picHeight = itemWidth * (line +1) + line * kMONENTS_PIC_SPACE;
}
}
monents.picHeight = picHeight;
return picHeight;
}
+ (CGFloat)monentsContentHeight:(MonentsInfoModel *)monents {
///计算文本的高度
if (!monents.contentAttribute) {
NSMutableAttributedString * attribute = [[NSMutableAttributedString alloc] init];
///置顶
if (monents.squareTop) {
UIImageView *imageView = [[UIImageView alloc]init];
imageView.frame = CGRectMake(0, 0, 36, 14);
imageView.image = [UIImage imageNamed:@"monents_info_top"];
NSMutableAttributedString * topAttrString = [NSMutableAttributedString yy_attachmentStringWithContent:imageView contentMode:UIViewContentModeScaleAspectFit attachmentSize:CGSizeMake(imageView.frame.size.width, imageView.frame.size.height) alignToFont:[UIFont systemFontOfSize:15.0] alignment:YYTextVerticalAlignmentCenter];
[attribute appendAttributedString:topAttrString];
}
///文本
NSString * text = monents.content;
if (text == nil || text.length <= 0) {
text = @"";
}
NSMutableAttributedString *contentAttribute = [[NSMutableAttributedString alloc] initWithString:text attributes:nil];
contentAttribute.yy_font = [UIFont systemFontOfSize:14];
NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphStyle alloc] init];
paraStyle.lineSpacing = 4.0f;//行间距
// 强制排版(从左到右)
paraStyle.alignment = NSTextAlignmentLeft;
paraStyle.baseWritingDirection = NSWritingDirectionLeftToRight;
contentAttribute.yy_paragraphStyle = paraStyle;
[attribute appendAttributedString:contentAttribute];
monents.contentAttribute = attribute;
}
YYTextContainer *container = [YYTextContainer new];
container.size = CGSizeMake(kMONENTS_CONTENT_MAX_WIDTH, CGFLOAT_MAX);
container.maximumNumberOfRows = 0;
YYTextLayout *layout = [YYTextLayout layoutWithContainer:container text:monents.contentAttribute];
CGFloat foldHeight = 0;
if (layout.rowCount > 6) {
foldHeight = kMONENTS_FOLD_HEIGHT;
if (monents.isFold) {
container.maximumNumberOfRows = 6;
} else {
container.maximumNumberOfRows = 0;
}
}
YYTextLayout *realLayout = [YYTextLayout layoutWithContainer:container text:monents.contentAttribute];
monents.contentHeight = realLayout.textBoundingSize.height;
return realLayout.textBoundingSize.height + foldHeight;
}
///计算评论的高度
+ (CGFloat)commentCommentRowHeight:(MonentsCommentModel * )comment {
if (comment.commentRowHeight > 0 && !comment.isReloadHeight) {
return comment.commentRowHeight;
}
__block CGFloat rowHeight = 0;
CGFloat commentTopHeight = 10 + 15 + 10;///10顶部的间隙 15 昵称的高度
NSString * content = comment.content.length > 0 ? comment.content : @"";
CGFloat commentHeight = [content boundingRectWithSize:CGSizeMake(KMONENTS_COMMENT_MAX_WIDTH, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingTruncatesLastVisibleLine attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15]} context:nil].size.height + 5;
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(KMONENTS_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