OfficialMessageModel.m 4.0 KB
//
//  OfficialMessageModel.m
//  DreamSleep
//
//  Created by peter on 2022/10/14.
//

// 官方动态内容显示最大行数
#define kMaxOfficialContentLine 5

#import "OfficialMessageModel.h"

@implementation OfficialMessageModel

#pragma mark - YYModel
+ (nullable NSDictionary<NSString *, id> *)modelCustomPropertyMapper {
    return @{@"message_id" : @"id"
    };
}

//- (BOOL)modelCustomTransformFromDictionary:(NSDictionary *)dic {
//    // YYModel给自定义字段设置默认值
//    // self.isOfficial = YES;
//    return YES;
//}

#pragma mark - public
+ (UIFont *)contentFont {
    return SysFont(14);
}

+ (CGFloat)contentMargin {
    return 15;
}

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

- (CGFloat)imgHeight {
    if (self.imgs) {
        if (iPhone5) {
            return 100;
        } else {
            NSArray *imgUrls = [self getImgUrlsArr:self.imgs];
            if (imgUrls.count == 1 || imgUrls.count == 2) {
                return 150;
            } else if (imgUrls.count == 3) {
                return 95;
            }
            return 0;
        }
    }
    return 0;
}

- (CGFloat)contentHeight {
    CGFloat contentW = [OfficialMessageModel contentMaxW];
    return [UILabel getHeightByWidth:contentW attributedText:[self contentAttriStr] font:[OfficialMessageModel contentFont]];
}

- (CGFloat)cellHeight {
    NSArray *imgUrls = [self getImgUrlsArr:self.imgs];
    CGFloat topH = 67;
    CGFloat bottomH = 15;
    CGFloat contentH = [self contentHeight];
    CGFloat imgTextSpace = imgUrls.count ? 12 : 0;
    CGFloat imgH = [self imgHeight];
    return topH + bottomH + contentH + imgTextSpace + imgH;
}

- (NSArray *)getImgUrlsArr:(NSString *)imgUrls {
    if (imgUrls && imgUrls.length) {
        return [imgUrls componentsSeparatedByString:@"|"];
    }
    return @[];
}

#pragma mark - private
+ (CGFloat)contentMaxW {
    return kScreenWidth - 4 * [OfficialMessageModel contentMargin];
}

- (NSArray *)contentMaxLines {
    return [self.content getLinesArrayOfStringWidth:[OfficialMessageModel contentMaxW] attributes:@{NSFontAttributeName : [OfficialMessageModel contentFont]}];
}

- (NSDictionary *)contentAttributes:(BOOL)isTail {
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.minimumLineHeight = [OfficialMessageModel contentFont].lineHeight;
    paragraphStyle.maximumLineHeight = [OfficialMessageModel contentFont].lineHeight;
    paragraphStyle.alignment = NSTextAlignmentLeft;
    NSMutableDictionary *mAttributes = [NSMutableDictionary dictionaryWithDictionary:
                                        @{NSFontAttributeName : [OfficialMessageModel contentFont],
                                          NSParagraphStyleAttributeName : paragraphStyle}];
    if (isTail) {
        [mAttributes setObject:BrandColor forKey:NSForegroundColorAttributeName];
    }
    return mAttributes.copy;
}

@end