/
ZHCMessagesBubbleCalculator.m
141 lines (101 loc) · 5.17 KB
/
ZHCMessagesBubbleCalculator.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
//
// ZHCMessagesBubbleCalculator.m
// ZHChat
//
// Created by aimoke on 16/8/11.
// Copyright © 2016年 zhuo. All rights reserved.
//
#import "ZHCMessagesBubbleCalculator.h"
#import "UIImage+ZHCMessages.h"
#import "ZHCMessagesTableviewLayoutAttributes.h"
#import "ZHCMessagesTableViewCell.h"
@interface ZHCMessagesBubbleCalculator()
@property (strong, nonatomic, readonly) NSCache *cache;
@property (assign, nonatomic, readonly) NSUInteger minimumBubbleWidth;
@property (assign, nonatomic, readonly) BOOL usesFixedWidthBubbles;
@property (assign, nonatomic, readonly) NSInteger additionalInset;
@property (assign, nonatomic) CGFloat layoutWidthForFixedWidthBubbles;
@end
@implementation ZHCMessagesBubbleCalculator
#pragma mark - Init
- (instancetype)initWithCache:(NSCache *)cache
minimumBubbleWidth:(NSUInteger)minimumBubbleWidth
usesFixedWidthBubbles:(BOOL)usesFixedWidthBubbles
{
NSParameterAssert(cache != nil);
NSParameterAssert(minimumBubbleWidth > 0);
self = [super init];
if (self) {
_cache = cache;
_minimumBubbleWidth = minimumBubbleWidth;
_usesFixedWidthBubbles = usesFixedWidthBubbles;
_layoutWidthForFixedWidthBubbles = 0.0f;
// this extra inset value is needed because `boundingRectWithSize:` is slightly off
// see comment below
_additionalInset = 4;
}
return self;
}
- (instancetype)init
{
NSCache *cache = [NSCache new];
cache.name = @"ZHCMessagesBubblesSizeCalculator.cache";
cache.countLimit = 200;
return [self initWithCache:cache
minimumBubbleWidth:[UIImage zhc_getBubbleCommpactImage].size.width
usesFixedWidthBubbles:NO];
}
#pragma mark - NSObject
- (NSString *)description
{
return [NSString stringWithFormat:@"<%@: cache=%@, minimumBubbleWidth=%@ usesFixedWidthBubbles=%@>",
[self class], self.cache, @(self.minimumBubbleWidth), @(self.usesFixedWidthBubbles)];
}
#pragma mark - Public Methds
-(void)prepareForResettingLayoutWithTableView:(ZHCMessagesTableView *)tableView
{
[self.cache removeAllObjects];
}
-(CGSize)messageBubbleSizeForMessageData:(id<ZHCMessageData>)messageData atIndexPath:(NSIndexPath *)indexPath withTableView:(ZHCMessagesTableView *)tableView
{
NSValue *cachedSize = [self.cache objectForKey:@([messageData messageHash])];
if (cachedSize != nil) {
return [cachedSize CGSizeValue];
}
CGSize finalSize = CGSizeZero;
if ([messageData isMediaMessage]) {
finalSize = [[messageData media] mediaViewDisplaySize];
}else{
ZHCMessagesTableviewLayoutAttributes *attributes = tableView.tableViewLayout;
CGFloat textViewHorizontallySpaceWithAvatar = attributes.textViewTextFrameInsets.right;
CGFloat bubbleWidth = attributes.messageBubbleContainerViewWidth;
CGFloat maxTextViewWidth = 0.0;
maxTextViewWidth = bubbleWidth - (textViewHorizontallySpaceWithAvatar + attributes.textViewTextContainerInsets.left + attributes.textViewTextContainerInsets.right);
CGRect stringRect = [[messageData text]boundingRectWithSize:CGSizeMake(maxTextViewWidth, CGFLOAT_MAX) options:(NSStringDrawingUsesFontLeading|NSStringDrawingUsesLineFragmentOrigin) attributes:@{ NSFontAttributeName : attributes.messageBubbleFont } context:nil];
// ZHCMessagesTableViewCell *cell = [tableView messageTableViewDequeueReusableCellWithIndexPath:indexPath];
// cell.textView.font = attributes.messageBubbleFont;
// CGSize stringSize = [cell.textView sizeThatFits:CGSizeMake(maxTextViewWidth, CGFLOAT_MAX)];
// stringSize.height = cell.textView.contentSize.height;
// NSLog(@"stringSize:%f",stringSize.height);
CGSize stringSize = CGRectIntegral(stringRect).size;
CGFloat verticalContainerInsets = attributes.textViewTextFrameInsets.top + attributes.textViewTextFrameInsets.bottom + attributes.textViewTextContainerInsets.bottom + attributes.textViewTextContainerInsets.top;
// same as above, an extra 2 points of magix
CGFloat verticalInsets = verticalContainerInsets + self.additionalInset;
CGFloat horizontalInsetsTotal = attributes.textViewTextFrameInsets.right + attributes.textViewTextFrameInsets.left + attributes.textViewTextContainerInsets.left + attributes.textViewTextContainerInsets.right;
CGFloat finalWidth = MAX(stringSize.width + horizontalInsetsTotal, self.minimumBubbleWidth) + self.additionalInset;
finalSize = CGSizeMake(finalWidth, stringSize.height + verticalInsets);
}
[self.cache setObject:[NSValue valueWithCGSize:finalSize] forKey:@([messageData messageHash])];
return finalSize;
}
#pragma mark Utilities
- (CGSize)zhc_avatarSizeForMessageData:(id<ZHCMessageData>)messageData withTableView:(ZHCMessagesTableView *)tableView
{
NSString *messageSender = [messageData senderId];
ZHCMessagesTableviewLayoutAttributes *attributes = tableView.tableViewLayout;
if ([messageSender isEqualToString:[tableView.dataSource senderId]]) {
return attributes.outgoingAvatarViewSize;
}
return attributes.incomingAvatarViewSize;
}
@end