107 lines
3.7 KiB
Objective-C
107 lines
3.7 KiB
Objective-C
//
|
||
// KBAIReplyModel.m
|
||
// keyBoard
|
||
//
|
||
// Created by Mac on 2026/1/16.
|
||
//
|
||
|
||
#import "KBAIReplyModel.h"
|
||
#import <MJExtension/MJExtension.h>
|
||
#import "KBAIReplyModel.h"
|
||
|
||
|
||
@implementation KBAIReplyModel
|
||
|
||
+ (NSDictionary *)mj_replacedKeyFromPropertyName {
|
||
return @{
|
||
@"replyId" : @"id",
|
||
@"userId" : @"userId",
|
||
@"userName" : @"userName",
|
||
@"avatarUrl" : @"userAvatar",
|
||
@"createTime" : @"createdAt",
|
||
};
|
||
}
|
||
|
||
- (void)setLiked:(NSInteger)liked {
|
||
// 后端返回的是 NSInteger (0/1),转换为 BOOL
|
||
_isLiked = (liked == 1);
|
||
}
|
||
|
||
- (void)setCreatedAt:(NSString *)createdAt {
|
||
// 后端返回的是字符串时间,转换为时间戳
|
||
if (createdAt && createdAt.length > 0) {
|
||
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
|
||
formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
|
||
formatter.timeZone = [NSTimeZone timeZoneWithName:@"Asia/Shanghai"];
|
||
NSDate *date = [formatter dateFromString:createdAt];
|
||
if (date) {
|
||
_createTime = [date timeIntervalSince1970];
|
||
}
|
||
}
|
||
}
|
||
|
||
- (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) + 内容区域 + 右边距(50)
|
||
// 内容区域宽度 = maxWidth - 68 - 28 - 8 - 50
|
||
CGFloat contentWidth = maxWidth - 68 - 28 - 8 - 50;
|
||
|
||
// 用户名高度(可能包含 "回复 @xxx")
|
||
NSMutableString *userNameText = [NSMutableString stringWithString:self.userName];
|
||
if (self.replyToUserName.length > 0) {
|
||
[userNameText appendFormat:@" 回复 @%@", self.replyToUserName];
|
||
}
|
||
UIFont *userNameFont = [UIFont systemFontOfSize:13 weight:UIFontWeightMedium];
|
||
CGRect userNameRect = [userNameText boundingRectWithSize:CGSizeMake(contentWidth, CGFLOAT_MAX)
|
||
options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading
|
||
attributes:@{NSFontAttributeName: userNameFont}
|
||
context:nil];
|
||
CGFloat userNameHeight = ceil(userNameRect.size.height);
|
||
|
||
// 内容高度
|
||
UIFont *contentFont = [UIFont systemFontOfSize:14];
|
||
CGRect contentRect = [self.content 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) + 内容 + 间距(6) + 时间 + 下边距(8)
|
||
CGFloat totalHeight = 8 + userNameHeight + 4 + contentHeight + 6 + timeHeight + 8;
|
||
|
||
// 最小高度(头像高度 + 上下边距)
|
||
CGFloat minHeight = 8 + 28 + 8;
|
||
totalHeight = MAX(totalHeight, minHeight);
|
||
|
||
self.cachedCellHeight = totalHeight;
|
||
return totalHeight;
|
||
}
|
||
|
||
@end
|