ArticleCell.m 5.3 KB
//
//  ArticleCell.m
//  DreamSleep
//
//  Created by peter on 2022/10/19.
//

#import "ArticleCell.h"

@interface ArticleCell ()
@property (nonatomic, strong) UILabel *titleLab;
@property (nonatomic, strong) UILabel *contentLab;
@property (nonatomic, strong) UIImageView *coverIV;
@property (nonatomic, strong) UIView *tagsView;
@property (nonatomic, strong) UILabel *readCountLab;
@end

@implementation ArticleCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self cornerRadius:12];
        self.dk_backgroundColorPicker = DKColorPickerWithKey(CornerViewBG);
        
        [self.contentView addSubview:self.titleLab];
        [self.contentView addSubview:self.contentLab];
        [self.contentView addSubview:self.coverIV];
        [self.contentView addSubview:self.tagsView];
        [self.contentView addSubview:self.readCountLab];

        [self.coverIV mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self.contentView).offset(-8);
            make.centerY.equalTo(self.contentView);
            make.size.mas_equalTo(CGSizeMake(86, 86));
        }];
        [self.titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.contentView).offset(8);
            make.top.equalTo(self.contentView).offset(12);
            make.right.equalTo(self.coverIV.mas_left).offset(-12);
        }];
        [self.contentLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(self.titleLab);
            make.top.equalTo(self.contentView).offset(43);
        }];
        [self.tagsView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(self.titleLab);
            make.height.equalTo(@21);
            make.bottom.equalTo(self.contentView).offset(-12);
        }];
        [self.readCountLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.equalTo(self.coverIV);
            make.height.equalTo(@25);
        }];
        [self layoutIfNeeded];
        [self.readCountLab setCornerRadiusRect:(UIRectCornerBottomLeft | UIRectCornerBottomRight) cornerRadius:12];
    }
    return self;
}

- (void)setModel:(ArticleModel *)model {
    _model = model;
    self.titleLab.text = model.title;
    self.contentLab.text = model.content;
    [self.coverIV yy_setImageWithURL:[NSURL URLWithString:model.cover] placeholder:[UIImage defaultPlaceholderWithSize:CGSizeMake(86, 86)]];
    self.readCountLab.text = [NSString stringWithFormat:@"%ld人阅读过", model.read_count];
    
    // tag标签最多只有3个
    NSArray *tagsTitleArr = model.tags ? [model.tags componentsSeparatedByString:@"|"] : @[];
    NSArray *tagTitleColors = @[BrandColor];
    NSArray *tagBgColors = @[ColorFromHexA(0x62C3D5, .2)];
    if (tagsTitleArr.count == 2) {
        tagTitleColors = @[BrandColor, ColorFromHex(0x9377FC)];
        tagBgColors = @[ColorFromHexA(0x62C3D5, .2), ColorFromHexA(0x9377FC, .2)];
    } else if (tagsTitleArr.count == 3) {
        tagTitleColors = @[BrandColor, ColorFromHex(0x9377FC), ColorFromHex(0x5E9EFC)];
        tagBgColors = @[ColorFromHexA(0x62C3D5, .2), ColorFromHexA(0x9377FC, .2), ColorFromHexA(0x5E9EFC, .2)];
    }
    [self.tagsView.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [obj removeFromSuperview];
    }];
    CGFloat totalW = 0;
    for (int idx = 0; idx < tagsTitleArr.count; idx++) {
        NSString *tagTitle = [NSString stringWithFormat:@"#%@", tagsTitleArr[idx]];
        CGFloat tagTitleW = [UILabel getWidthWithText:tagTitle font:SysFont(12)] + 16;
        UILabel *tagLab = [UILabel labWithTextColor:tagTitleColors[idx] font:SysFont(12)];
        tagLab.text = tagTitle;
        tagLab.textAlignment = NSTextAlignmentCenter;
        tagLab.backgroundColor = tagBgColors[idx];
        [tagLab cornerRadius:10.5];
        tagLab.frame = CGRectMake(totalW + idx * 8, 0, tagTitleW, 21);
        totalW += tagTitleW;
        [self.tagsView addSubview:tagLab];
    }
    return;
}

#pragma mark - lazy
- (UILabel *)titleLab {
    if (!_titleLab) {
        _titleLab = [UILabel labWithFont:BoldFont(15)];
        _titleLab.dk_textColorPicker = DKColorPickerWithKey(Dk_TITLE);
    }
    return _titleLab;
}

- (UILabel *)contentLab {
    if (!_contentLab) {
        _contentLab = [UILabel labWithFont:SysFont(14)];
        _contentLab.dk_textColorPicker = DKColorPickerWithColors(SubTitleColor, ColorFromHexA(0xFFFFFF, .5), DSWhite);
    }
    return _contentLab;
}

- (UIImageView *)coverIV {
    if (!_coverIV) {
        _coverIV = [UIImageView new];
        [_coverIV cornerRadius:12];
        _coverIV.dk_alphaPicker = DKAlphaPickerWithAlphas(1.f, 0.5f, 0.1f);
    }
    return _coverIV;
}

- (UIView *)tagsView {
    if (!_tagsView) {
        _tagsView = [UIView new];
        _tagsView.clipsToBounds = YES;
    }
    return _tagsView;
}

- (UILabel *)readCountLab {
    if (!_readCountLab) {
        _readCountLab = [UILabel labWithFont:SysFont(11)];
        _readCountLab.textAlignment = NSTextAlignmentCenter;
        _readCountLab.textColor = ColorFromHexA(0xFFFFFF, .7);
        _readCountLab.backgroundColor = ColorFromHexA(0x161E38, .35);
    }
    return _readCountLab;
}

@end