ComReplyModel.m 4.5 KB
//
//  ComReplyModel.m
//  DreamSleep
//
//  Created by peter on 2022/10/8.
//

#import "ComReplyModel.h"

// 回复内容显示最大行数
#define kMaxReplyLine 2

@implementation ComReplyModel

#pragma mark - 模型字段映射
+ (nullable NSDictionary<NSString *, id> *)modelCustomPropertyMapper {
    return @{@"commentModel" : @"comment",
             @"replyModel" : @"reply"
    };
}

#pragma mark - public
- (NSString *)replyNickName {
    return [NSString stringWithFormat:@"%@:", self.replyModel.nick_name];
}

+ (UIFont *)commentContentFont {
    return SysFont(14);
}

+ (UIFont *)replyNickNameFont {
    return SysFont(15);
}

+ (UIFont *)replyContentFont {
    return SysFont(14);
}

- (NSAttributedString *)replyContentAttriStr {
    NSString *replyContentStr = self.replyModel.content ? self.replyModel.content : @"";
    NSMutableAttributedString *replyAttStr = [[NSMutableAttributedString alloc] initWithString:replyContentStr attributes:[self replyContentAttributes:NO]];
    
    if ([self replyContentMaxLines].count > kMaxReplyLine) {
        // 1、获取显示最多行字符串
        NSMutableString *mLineStr = [NSMutableString string];
        for (NSInteger i = 0; i < kMaxReplyLine; i++) {
            [mLineStr appendString:[self replyContentMaxLines][i]];
        }
        // 2、截取字符串最后1个位置,用于填充自定义富文本
        NSString * subLineStr = [mLineStr substringWithRange:NSMakeRange(0, mLineStr.length - 1)];
        // 3、将subLineStr转换未富文本
        replyAttStr = [[NSMutableAttributedString alloc] initWithString:subLineStr attributes:[self replyContentAttributes:NO]];
        // 4、自定义结尾富文本
        NSMutableAttributedString *dotAtrStr = [[NSMutableAttributedString alloc] initWithString:@"..." attributes:[self replyContentAttributes:YES]];
        [replyAttStr appendAttributedString:dotAtrStr.copy];
    }
    
    return replyAttStr.copy;
}

- (CGFloat)commentContentLeftMargin {
    return 84;
}

- (CGFloat)commentContentRightMargin {
    return 30;
}

- (CGFloat)commentContentHeight {
    CGFloat commentContentW = kScreenWidth - [self commentContentLeftMargin] - [self commentContentRightMargin];
    NSString *content = self.commentModel.content;
    return [UILabel getHeightByWidth:commentContentW text:content font:[ComReplyModel commentContentFont]];
}

- (CGFloat)replyNickNameW {
    CGFloat replyNameLabW = [UILabel getWidthWithText:[self replyNickName] font:[ComReplyModel replyNickNameFont]];
    return replyNameLabW > 75 ? 75 : replyNameLabW;
}

- (CGSize)replyContentSize {
    CGFloat replyContentW = [self replyContentMaxW];
    CGFloat replyContentH = [UILabel getHeightByWidth:replyContentW attributedText:[self replyContentAttriStr] font:[ComReplyModel replyContentFont]];
    return CGSizeMake(replyContentW, replyContentH);
}

- (CGFloat)replyViewHeight {
    CGFloat replayViewH = 0;
    if (self.replyModel) {
        replayViewH = 9 + [self replyContentSize].height + (self.commentModel.total_replys > 1 ? 37 : 9);
    }
    return replayViewH;
}

- (CGFloat)commentCellHeight {
    CGFloat topH = 48;
    CGFloat commentH = [self commentContentHeight];
    CGFloat replayViewH = [self replyViewHeight] + (self.replyModel ? 12 : 0);
    CGFloat bottomMargin = 15;
    return topH + commentH + replayViewH + bottomMargin;
}

#pragma mark - private
- (NSArray *)replyContentMaxLines {
    return [self.replyModel.content getLinesArrayOfStringWidth:[self replyContentMaxW] attributes:@{NSFontAttributeName : [ComReplyModel replyContentFont]}];
}

- (CGFloat)replyContentMaxW {
    return kScreenWidth - [self commentContentLeftMargin] - [self commentContentRightMargin] - [self replyNickNameW] - 16;
}

- (NSDictionary *)replyContentAttributes:(BOOL)isTail {
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.minimumLineHeight = [ComReplyModel replyContentFont].lineHeight;
    paragraphStyle.maximumLineHeight = [ComReplyModel replyContentFont].lineHeight;
    paragraphStyle.alignment = NSTextAlignmentLeft;
    NSMutableDictionary *mAttributes = [NSMutableDictionary dictionaryWithDictionary:
                                        @{NSFontAttributeName : [ComReplyModel replyContentFont],
                                          NSParagraphStyleAttributeName : paragraphStyle}];
    // 只有1条回复高亮...
    if (isTail && self.commentModel.total_replys < 2) {
        [mAttributes setObject:BrandColor forKey:NSForegroundColorAttributeName];
    }
    return mAttributes.copy;
}

@end