Files
peko-ios/YuMi/Modules/YMMonents/Presenter/XPMonentsLayoutConfig.m
2023-08-10 18:44:46 +08:00

318 lines
12 KiB
Objective-C

//
// YMMonentsLayoutConfig.m
// YUMI
//
// Created by YUMI on 2022/5/12.
//
#import "XPMonentsLayoutConfig.h"
#import <YYText/YYText.h>
#import "DJDKMIMOMColor.h"
///Model
#import "MonentsInfoModel.h"
#import "MonentsCommentModel.h"
#import "QEmotionHelper.h"
@implementation XPMonentsLayoutConfig
#pragma mark - 审核的布局
+ (void)getNewlayoutMonentsModelWithDynamic:(MonentsInfoModel *)monents {
CGFloat rowHeight = 10;
///用户信息和文字之间的间隙
rowHeight += kMONENTS_CONTENT_SPACAE_HEIGHT;
///文字内容的高度
rowHeight += [self monentsContentHeight:monents];
///图片的高度
rowHeight += [self getNewMonentsPicHeight:monents];
///如果没有图片的话 间隙就只有一个
if (monents.type == MonentsContentType_Picture && monents.content.length > 0) {
rowHeight += (kMONENTS_CONTENT_SPACAE_HEIGHT);
}
///话题的高度
if (monents.worldId > 0) {
rowHeight += kMONENTS_TEXT_TOPIC_HEIGHT;
}
///底部操作栏的高度
rowHeight += kMONENTS_TOOL_BAR_HEIGHT;
monents.rowHeight = rowHeight;
}
+ (CGFloat)getNewMonentsPicHeight:(MonentsInfoModel *)monents {
///计算图片的高度
NSInteger picCount = monents.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 - kMONENTS_PIC_SPACE) / 2;
} else {
if (picCount > 9) {
picCount = 9;
}
CGFloat itemWidth = (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;
}
+ (void)layoutMonentsModelWithDynamic:(MonentsInfoModel *)monents {
CGFloat rowHeight = 10;
///用户信息和文字之间的间隙
rowHeight += kMONENTS_CONTENT_SPACAE_HEIGHT;
///文字内容的高度
rowHeight += [self monentsContentHeight:monents];
///图片的高度
rowHeight += [self monentsPicHeight:monents];
///如果没有图片的话 间隙就只有一个
if (monents.type == MonentsContentType_Picture && monents.content.length > 0) {
rowHeight += (kMONENTS_CONTENT_SPACAE_HEIGHT);
}
///话题的高度
if (monents.worldId > 0) {
rowHeight += kMONENTS_TEXT_TOPIC_HEIGHT;
}
///底部操作栏的高度
rowHeight += kMONENTS_TOOL_BAR_HEIGHT;
monents.rowHeight = rowHeight;
}
+ (void)layoutSimpleMonentsModel:(MonentsInfoModel *)monents {
CGFloat rowHeight = kMONENTS_CONTENT_SPACAE_HEIGHT;
///文字内容的高度
rowHeight += [self simpleMonentsContentHeight:monents];
///图片的高度
rowHeight += [self monentsPicHeight:monents];
///如果没有图片的话 间隙就只有一个
if (monents.type == MonentsContentType_Picture && monents.content.length > 0) {
rowHeight += (kMONENTS_CONTENT_SPACAE_HEIGHT * 2);
}else {
rowHeight += kMONENTS_CONTENT_SPACAE_HEIGHT;
}
///话题的高度
rowHeight += kMONENTS_TEXT_TOPIC_HEIGHT;
monents.rowHeight = rowHeight;
}
+ (CGFloat)simpleMonentsContentHeight:(MonentsInfoModel *)monents {
if (monents.content.length <= 0) {
monents.contentHeight = 0;
return 0;
}
///计算文本的高度
if (!monents.contentAttribute) {
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] init];
if (monents.squareTop ) {//动态/广场
NSTextAttachment * attachment = [[NSTextAttachment alloc] init];
attachment.bounds = CGRectMake(0, 0, 25 * 1.3 ,10 * 1.3);
attachment.image = [UIImage imageNamed:@"monents_info_top"];
NSAttributedString * starAttribute = [NSMutableAttributedString attributedStringWithAttachment:(NSTextAttachment *)attachment];
[attributedString insertAttributedString:starAttribute atIndex:0];
}
[attributedString appendAttributedString:[self creatStrAttrByStr:monents.content attributed:@{NSFontAttributeName : [UIFont systemFontOfSize:15]}]];
attributedString.yy_lineSpacing = 5;
attributedString.yy_lineBreakMode = NSLineBreakByWordWrapping;
monents.contentAttribute = attributedString;
}
YYTextContainer *container = [YYTextContainer new];
container.size = CGSizeMake(kSIMPLE_MONENTS_CONTENT_MAX_WIDTH, CGFLOAT_MAX);
container.maximumNumberOfRows = 0;
YYTextLayout *layout = [YYTextLayout layoutWithContainer:container text:monents.contentAttribute];
monents.numberOfText = layout.rowCount;
CGFloat foldHeight = 0;
if (layout.rowCount > 6) {
foldHeight = kMONENTS_FOLD_HEIGHT;
NSInteger numberOfLines = monents.isFold ? 6 : 0;
container.maximumNumberOfRows = numberOfLines;
}
YYTextLayout *realLayout = [YYTextLayout layoutWithContainer:container text:monents.contentAttribute];
CGFloat contentHeight = realLayout.textBoundingSize.height;
monents.contentHeight =contentHeight;
return contentHeight + foldHeight;
}
+ (CGFloat)simpleMonentsPicHeight:(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 = (kSIMPLE_MONENTS_CONTENT_MAX_WIDTH - kMONENTS_PIC_SPACE) / 2;
} else {
if (picCount > 9) {
picCount = 9;
}
CGFloat itemWidth = (kSIMPLE_MONENTS_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;
}
#pragma mark - 正常的布局
+ (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 && monents.content.length > 0) {
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.content.length <= 0) {
monents.contentHeight = 0;
return 0;
}
///计算文本的高度
if (!monents.contentAttribute) {
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] init];
if (monents.squareTop ) {//动态/广场
NSTextAttachment * attachment = [[NSTextAttachment alloc] init];
attachment.bounds = CGRectMake(0, 0, 25 * 1.3 ,10 * 1.3);
attachment.image = [UIImage imageNamed:@"monents_info_top"];
NSAttributedString * starAttribute = [NSMutableAttributedString attributedStringWithAttachment:(NSTextAttachment *)attachment];
[attributedString insertAttributedString:starAttribute atIndex:0];
}
[attributedString appendAttributedString:[self creatStrAttrByStr:monents.content attributed:@{NSFontAttributeName : [UIFont systemFontOfSize:15]}]];
attributedString.yy_lineSpacing = 5;
attributedString.yy_lineBreakMode = NSLineBreakByWordWrapping;
monents.contentAttribute = attributedString;
}
YYTextContainer *container = [YYTextContainer new];
container.size = CGSizeMake(kMONENTS_CONTENT_MAX_WIDTH, CGFLOAT_MAX);
container.maximumNumberOfRows = 0;
YYTextLayout *layout = [YYTextLayout layoutWithContainer:container text:monents.contentAttribute];
monents.numberOfText = layout.rowCount;
CGFloat foldHeight = 0;
if (layout.rowCount > 6) {
foldHeight = kMONENTS_FOLD_HEIGHT;
NSInteger numberOfLines = monents.isFold ? 6 : 0;
container.maximumNumberOfRows = numberOfLines;
}
YYTextLayout *realLayout = [YYTextLayout layoutWithContainer:container text:monents.contentAttribute];
CGFloat contentHeight = realLayout.textBoundingSize.height;
monents.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(KMONENTS_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(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