Commit c54ebf24 cgx

优化社区动态列表个人动态cell高度显示

1 个父辈 b8563e0e
...@@ -36,13 +36,16 @@ NS_ASSUME_NONNULL_BEGIN ...@@ -36,13 +36,16 @@ NS_ASSUME_NONNULL_BEGIN
@property (nonatomic, assign) int likeCount; @property (nonatomic, assign) int likeCount;
/// 评论数 /// 评论数
@property (nonatomic, assign) int remarkCount; @property (nonatomic, assign) int remarkCount;
/// 社区动态cell内容(不展开)和社区详情cell内容(全部展开)展开标识
@property (nonatomic, assign) DynModelType modelType;
+ (UIFont *)contentFont;
+ (CGFloat)contentMargin:(DynModelType)type;
- (NSAttributedString *)contentAttriStr;
/// 文本显示高度
- (CGFloat)contentHeight:(DynModelType)type;
/// 图片高度
- (CGFloat)imgHeight; - (CGFloat)imgHeight;
/// 动态cell高度 - (CGFloat)contentHeight;
- (CGFloat)cellHeight:(DynModelType)type; - (CGFloat)cellHeight;
- (NSArray *)getImgUrlsArr:(NSString *)imgUrls; - (NSArray *)getImgUrlsArr:(NSString *)imgUrls;
@end @end
......
...@@ -7,6 +7,9 @@ ...@@ -7,6 +7,9 @@
#import "ComDynModel.h" #import "ComDynModel.h"
// 用户动态内容显示最大行数
#define kMaxUserDynContentLine 5
@implementation ComDynModel @implementation ComDynModel
+ (nullable NSDictionary<NSString *, id> *)modelCustomPropertyMapper { + (nullable NSDictionary<NSString *, id> *)modelCustomPropertyMapper {
...@@ -22,6 +25,12 @@ ...@@ -22,6 +25,12 @@
}; };
} }
- (BOOL)modelCustomTransformFromDictionary:(NSDictionary *)dic {
// YYModel给自定义字段设置默认值
self.modelType = DynModelTypeCom;
return YES;
}
//- (BOOL)modelCustomTransformFromDictionary:(NSDictionary *)dic { //- (BOOL)modelCustomTransformFromDictionary:(NSDictionary *)dic {
// NSNumber *timestamp = dic[@"is_praise"]; // NSNumber *timestamp = dic[@"is_praise"];
// if (![timestamp isKindOfClass:[NSNumber class]]) return NO; // if (![timestamp isKindOfClass:[NSNumber class]]) return NO;
...@@ -30,11 +39,37 @@ ...@@ -30,11 +39,37 @@
//} //}
#pragma mark - public #pragma mark - public
- (CGFloat)contentHeight:(DynModelType)type { + (UIFont *)contentFont {
CGFloat contentW = kScreenWidth - 60; return SysFont(14);
NSString *content = self.content; }
CGFloat contentH = [UILabel getHeightByWidth:contentW text:content font:SysFont(14)];
return type == DynModelTypeCom ? (contentH > 100 ? 100 : contentH) : contentH; + (CGFloat)contentMargin:(DynModelType)type {
return type == DynModelTypeCom ? 15 : 30;
}
- (NSAttributedString *)contentAttriStr {
NSString *contentStr = self.content ?: @"";
NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc] initWithString:contentStr attributes:[self contentAttributes:NO]];
if (self.modelType == DynModelTypeDetail) {
return attStr.copy;
}
if ([self contentMaxLines].count > kMaxUserDynContentLine) {
// 1、获取显示最多行字符串
NSMutableString *mLineStr = [NSMutableString string];
for (NSInteger i = 0; i < kMaxUserDynContentLine; 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 { - (CGFloat)imgHeight {
...@@ -54,11 +89,16 @@ ...@@ -54,11 +89,16 @@
return 0; return 0;
} }
- (CGFloat)cellHeight:(DynModelType)type { - (CGFloat)contentHeight {
CGFloat contentW = [self contentMaxW];
return [UILabel getHeightByWidth:contentW attributedText:[self contentAttriStr] font:[ComDynModel contentFont]];
}
- (CGFloat)cellHeight {
NSArray *imgUrls = [self getImgUrlsArr:self.imgUrls]; NSArray *imgUrls = [self getImgUrlsArr:self.imgUrls];
CGFloat topH = 67; CGFloat topH = 67;
CGFloat bottomH = type == DynModelTypeCom ? (imgUrls.count ? 52 : 56) : 10; CGFloat bottomH = self.modelType == DynModelTypeDetail ? 10 : (imgUrls.count ? 52 : 56);
CGFloat contentH = [self contentHeight:type]; CGFloat contentH = [self contentHeight];
CGFloat imgTextSpace = imgUrls.count ? 12 : 0; CGFloat imgTextSpace = imgUrls.count ? 12 : 0;
CGFloat imgH = [self imgHeight]; CGFloat imgH = [self imgHeight];
return topH + bottomH + contentH + imgTextSpace + imgH; return topH + bottomH + contentH + imgTextSpace + imgH;
...@@ -71,4 +111,33 @@ ...@@ -71,4 +111,33 @@
return @[]; return @[];
} }
#pragma mark - private
- (CGFloat)contentMaxW {
CGFloat marigin = [ComDynModel contentMargin:self.modelType];
if (self.modelType == DynModelTypeCom) {
return kScreenWidth - 4 * marigin;
} else if (self.modelType == DynModelTypeDetail) {
return kScreenWidth - 2 * marigin;
}
return 0;
}
- (NSArray *)contentMaxLines {
return [self.content getLinesArrayOfStringWidth:[self contentMaxW] attributes:@{NSFontAttributeName : [ComDynModel contentFont]}];
}
- (NSDictionary *)contentAttributes:(BOOL)isTail {
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.minimumLineHeight = [ComDynModel contentFont].lineHeight;
paragraphStyle.maximumLineHeight = [ComDynModel contentFont].lineHeight;
paragraphStyle.alignment = NSTextAlignmentLeft;
NSMutableDictionary *mAttributes = [NSMutableDictionary dictionaryWithDictionary:
@{NSFontAttributeName : [ComDynModel contentFont],
NSParagraphStyleAttributeName : paragraphStyle}];
if (isTail) {
[mAttributes setObject:BrandColor forKey:NSForegroundColorAttributeName];
}
return mAttributes.copy;
}
@end @end
...@@ -5,7 +5,7 @@ ...@@ -5,7 +5,7 @@
// Created by peter on 2022/10/14. // Created by peter on 2022/10/14.
// //
// 回复内容显示最大行数 // 官方动态内容显示最大行数
#define kMaxOfficialContentLine 5 #define kMaxOfficialContentLine 5
#import "OfficialMessageModel.h" #import "OfficialMessageModel.h"
......
...@@ -10,7 +10,6 @@ ...@@ -10,7 +10,6 @@
#import "LikeButton.h" #import "LikeButton.h"
@interface ComDynamicCell () @interface ComDynamicCell ()
@property (nonatomic, assign) DynModelType modelType;
@property (nonatomic, assign) CGFloat margin; @property (nonatomic, assign) CGFloat margin;
@property (nonatomic, strong) UIImageView *userIcon; @property (nonatomic, strong) UIImageView *userIcon;
@property (nonatomic, strong) UILabel *userNameLab; @property (nonatomic, strong) UILabel *userNameLab;
...@@ -28,12 +27,12 @@ ...@@ -28,12 +27,12 @@
#pragma mark - init #pragma mark - init
- (instancetype)initWithCellType:(DynModelType)type style:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { - (instancetype)initWithCellType:(DynModelType)type style:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) { if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
self.modelType = type;
self.selectionStyle = UITableViewCellSelectionStyleNone; self.selectionStyle = UITableViewCellSelectionStyleNone;
self.photos = [NSMutableArray array]; self.photos = [NSMutableArray array];
self.margin = [ComDynModel contentMargin:type];
[self.contentView addSubview:self.userIcon]; [self.contentView addSubview:self.userIcon];
[self.contentView addSubview:self.userNameLab]; [self.contentView addSubview:self.userNameLab];
[self.contentView addSubview:self.timeLab]; [self.contentView addSubview:self.timeLab];
...@@ -43,12 +42,10 @@ ...@@ -43,12 +42,10 @@
[self.contentView addSubview:self.likeBtn]; [self.contentView addSubview:self.likeBtn];
[self.contentView addSubview:self.remarkCountBtn]; [self.contentView addSubview:self.remarkCountBtn];
self.margin = 15;
if (type == DynModelTypeCom) { if (type == DynModelTypeCom) {
[self cornerRadius:24.0]; [self cornerRadius:24.0];
self.dk_backgroundColorPicker = DKColorPickerWithColors(DSWhite, CornerViewDarkColor, DSWhite); self.dk_backgroundColorPicker = DKColorPickerWithColors(DSWhite, CornerViewDarkColor, DSWhite);
} else { } else {
self.margin = 30;
[self hideNeedlessView]; [self hideNeedlessView];
self.backgroundColor = DSClearColor; self.backgroundColor = DSClearColor;
} }
...@@ -107,15 +104,15 @@ ...@@ -107,15 +104,15 @@
[self.userIcon yy_setImageWithURL:[NSURL URLWithString:model.userIcon] placeholder:[UIImage defaultPlaceholderWithSize:CGSizeMake(40, 40)]]; [self.userIcon yy_setImageWithURL:[NSURL URLWithString:model.userIcon] placeholder:[UIImage defaultPlaceholderWithSize:CGSizeMake(40, 40)]];
self.userNameLab.text = model.userName; self.userNameLab.text = model.userName;
self.timeLab.text = model.time; self.timeLab.text = model.time;
self.contentLab.text = model.content; self.contentLab.attributedText = [model contentAttriStr];
[self.likeBtn updateLikeBtnState:model.isLike likeCount:model.likeCount]; [self.likeBtn updateLikeBtnState:model.isLike likeCount:model.likeCount];
[self.remarkCountBtn setTitle:[NSString stringWithFormat:@"%d", model.remarkCount] forState:UIControlStateNormal]; [self.remarkCountBtn setTitle:[NSString stringWithFormat:@"%d", model.remarkCount] forState:UIControlStateNormal];
[self.contentLab mas_remakeConstraints:^(MASConstraintMaker *make) { [self.contentLab mas_remakeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.userIcon.mas_bottom).offset(12); make.top.equalTo(self.userIcon.mas_bottom).offset(12);
make.left.equalTo(self.contentView).offset(self.margin); make.left.equalTo(self.contentView).offset(self.margin);
make.right.equalTo(self.contentView).offset(-self.margin); make.right.equalTo(self.contentView).offset(-self.margin);
make.height.equalTo(@([model contentHeight:self.modelType])); make.height.equalTo(@([model contentHeight]));
}]; }];
// 先移除 // 先移除
......
...@@ -94,7 +94,7 @@ ...@@ -94,7 +94,7 @@
return [model cellHeight]; return [model cellHeight];
} else { } else {
ComDynModel *model = self.comListVM.listArr[indexPath.section]; ComDynModel *model = self.comListVM.listArr[indexPath.section];
return [model cellHeight:DynModelTypeCom]; return [model cellHeight];
} }
} }
......
支持 Markdown 格式
你添加了 0 到此讨论。请谨慎行事。
Finish editing this message first!