Files
keyboard/keyBoard/Class/AiTalk/M/KBAIReplyModel.m
2026-01-16 19:09:54 +08:00

79 lines
2.6 KiB
Objective-C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// KBAIReplyModel.m
// keyBoard
//
// Created by Mac on 2026/1/16.
//
#import "KBAIReplyModel.h"
#import <MJExtension/MJExtension.h>
@implementation KBAIReplyModel
+ (NSDictionary *)mj_replacedKeyFromPropertyName {
return @{
@"replyId" : @"id",
@"userName" : @[ @"userName", @"nickname", @"name" ],
@"avatarUrl" : @[ @"avatarUrl", @"avatar" ],
};
}
- (NSString *)formattedTime {
NSDate *date = [NSDate dateWithTimeIntervalSince1970:self.createTime];
NSTimeInterval interval = [[NSDate date] timeIntervalSinceDate:date];
if (interval < 60) {
return @"刚刚";
} else if (interval < 3600) {
return [NSString stringWithFormat:@"%.0f分钟前", interval / 60];
} else if (interval < 86400) {
return [NSString stringWithFormat:@"%.0f小时前", interval / 3600];
} else if (interval < 86400 * 30) {
return [NSString stringWithFormat:@"%.0f天前", interval / 86400];
} else {
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = @"MM-dd";
return [formatter stringFromDate:date];
}
}
- (CGFloat)calculateCellHeightWithMaxWidth:(CGFloat)maxWidth {
if (self.cachedCellHeight > 0) {
return self.cachedCellHeight;
}
// Cell 布局:
// 左边距(68) + 头像(28) + 间距(8) + 内容区域 + 间距(8) + 点赞按钮(40) + 右边距(16)
// 内容区域宽度 = maxWidth - 68 - 28 - 8 - 8 - 40 - 16
CGFloat contentWidth = maxWidth - 68 - 28 - 8 - 8 - 40 - 16;
// 构建富文本计算高度
NSMutableString *fullText = [NSMutableString stringWithString:self.userName];
if (self.replyToUserName.length > 0) {
[fullText appendFormat:@" 回复 @%@", self.replyToUserName];
}
[fullText appendFormat:@"%@", self.content];
UIFont *contentFont = [UIFont systemFontOfSize:14];
CGRect contentRect = [fullText boundingRectWithSize:CGSizeMake(contentWidth, CGFLOAT_MAX)
options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading
attributes:@{NSFontAttributeName: contentFont}
context:nil];
CGFloat contentHeight = ceil(contentRect.size.height);
// 时间高度(单行)
CGFloat timeHeight = 14; // 11号字体
// 总高度 = 上边距(8) + 内容 + 间距(4) + 时间 + 下边距(8)
CGFloat totalHeight = 8 + contentHeight + 4 + timeHeight + 8;
// 最小高度(头像高度 + 上下边距)
CGFloat minHeight = 8 + 28 + 8;
totalHeight = MAX(totalHeight, minHeight);
self.cachedCellHeight = totalHeight;
return totalHeight;
}
@end