Files
keyboard/keyBoard/Class/AiTalk/M/KBAIReplyModel.m

85 lines
3.1 KiB
Mathematica
Raw Normal View History

2026-01-16 15:55:08 +08:00
//
// 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];
}
}
2026-01-16 19:09:54 +08:00
- (CGFloat)calculateCellHeightWithMaxWidth:(CGFloat)maxWidth {
if (self.cachedCellHeight > 0) {
return self.cachedCellHeight;
}
// Cell
2026-01-16 20:31:42 +08:00
// (68) + (28) + (8) + + (50)
// = maxWidth - 68 - 28 - 8 - 50
CGFloat contentWidth = maxWidth - 68 - 28 - 8 - 50;
2026-01-16 19:09:54 +08:00
2026-01-16 20:31:42 +08:00
// "回复 @xxx"
NSMutableString *userNameText = [NSMutableString stringWithString:self.userName];
2026-01-16 19:09:54 +08:00
if (self.replyToUserName.length > 0) {
2026-01-16 20:31:42 +08:00
[userNameText appendFormat:@" 回复 @%@", self.replyToUserName];
2026-01-16 19:09:54 +08:00
}
2026-01-16 20:31:42 +08:00
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);
2026-01-16 19:09:54 +08:00
2026-01-16 20:31:42 +08:00
//
2026-01-16 19:09:54 +08:00
UIFont *contentFont = [UIFont systemFontOfSize:14];
2026-01-16 20:31:42 +08:00
CGRect contentRect = [self.content boundingRectWithSize:CGSizeMake(contentWidth, CGFLOAT_MAX)
options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading
attributes:@{NSFontAttributeName: contentFont}
context:nil];
2026-01-16 19:09:54 +08:00
CGFloat contentHeight = ceil(contentRect.size.height);
//
CGFloat timeHeight = 14; // 11
2026-01-16 20:31:42 +08:00
// = (8) + + (4) + + (6) + + (8)
CGFloat totalHeight = 8 + userNameHeight + 4 + contentHeight + 6 + timeHeight + 8;
2026-01-16 19:09:54 +08:00
// +
CGFloat minHeight = 8 + 28 + 8;
totalHeight = MAX(totalHeight, minHeight);
self.cachedCellHeight = totalHeight;
return totalHeight;
}
2026-01-16 15:55:08 +08:00
@end