-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathMEOutgoingMessageCollectionViewCell.m
More file actions
executable file
·120 lines (101 loc) · 4.58 KB
/
Copy pathMEOutgoingMessageCollectionViewCell.m
File metadata and controls
executable file
·120 lines (101 loc) · 4.58 KB
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
//
// MEAtlasCollectionViewCell.m
// Atlas Messenger
//
// Created by steve on 2/27/16.
// Copyright © 2016 Layer, Inc. All rights reserved.
//
#import "MEOutgoingMessageCollectionViewCell.h"
#import "METextInputView.h"
@implementation MEOutgoingMessageCollectionViewCell
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self meCommonInit];
}
return self;
}
- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self) {
[self meCommonInit];
}
return self;
}
-(void)meCommonInit {
self.bubbleView = [[UIView alloc] initWithFrame:CGRectZero];
self.bubbleView.layer.cornerRadius = 16;
[self.contentView addSubview:self.bubbleView];
[self.contentView sendSubviewToBack:self.bubbleView];
self.avatarImageView = [[ATLAvatarImageView alloc] init];
self.avatarImageView.translatesAutoresizingMaskIntoConstraints = NO;
self.avatarImageView.hidden = YES;
self.avatarImageView.frame = CGRectZero;
[self.contentView addSubview:self.avatarImageView];
[self.contentView bringSubviewToFront:self.avatarImageView];
}
- (void)updateWithSender:(id<ATLParticipant>)sender {
if (sender) {
self.avatarImageView.hidden = NO;
self.avatarImageView.avatarItem = sender;
} else {
self.avatarImageView.hidden = YES;
}
}
- (void)shouldDisplayAvatarItem:(BOOL)shouldDisplayAvatarItem {
self.shouldDisplayAvatar = shouldDisplayAvatarItem;
}
- (void)presentMessage:(LYRMessage *)message {
self.bubbleView.backgroundColor = [[ATLOutgoingMessageCollectionViewCell appearance] bubbleViewColor];
LYRMessagePart *part = message.parts[0];
if ([part.MIMEType isEqualToString:@"text/plain"]) {
NSString * messageString = [[NSString alloc] initWithData:part.data encoding:NSUTF8StringEncoding];
NSString * messageHTML = [METextInputView convertSubstituedToHTML:messageString];
UIColor * messageTextColor = [[ATLOutgoingMessageCollectionViewCell appearance] messageTextColor];
NSString * fontSize = [NSString stringWithFormat:@"font-size:%ipx;", 16];
NSString * fontColor = [NSString stringWithFormat:@"color:%@;", [self hexStringFromColor:messageTextColor]];
messageHTML = [messageHTML stringByReplacingOccurrencesOfString:@"font-size:16px;" withString:fontSize];
messageHTML = [messageHTML stringByReplacingOccurrencesOfString:@"color:#000000;" withString:fontColor];
[self setHTMLString:messageHTML];
messageString = nil;
}
}
-(void)layoutSubviews {
[super layoutSubviews];
self.avatarImageView.frame = CGRectMake(self.contentView.frame.size.width-27-ATLMessageBubbleLabelHorizontalPadding, ATLMessageBubbleLabelVerticalPadding, 27, 27);
if (!self.superview) { return; }
if (self.shouldDisplayAvatar == NO) { self.avatarImageView.frame = CGRectZero; }
CGFloat maxBubbleWidth = ATLMaxCellWidth() + (ATLMessageBubbleLabelHorizontalPadding*2);
CGFloat textHeight = self.contentView.frame.size.height-(ATLMessageBubbleLabelVerticalPadding*2);
CGSize textSize = [self.messageView suggestedSizeForTextForSize:CGSizeMake(ATLMaxCellWidth(), textHeight)];
CGFloat bubbleWidth = maxBubbleWidth;
textSize.width += (ATLMessageBubbleLabelHorizontalPadding * 2);
if (textSize.width < maxBubbleWidth) { bubbleWidth = textSize.width; }
CGFloat leadIn = self.contentView.frame.size.width - bubbleWidth - ATLMessageCellHorizontalMargin - self.avatarImageView.frame.size.width;
self.bubbleView.frame = CGRectMake(leadIn, 0, bubbleWidth, self.contentView.frame.size.height);
self.messageView.frame = CGRectMake(leadIn+ATLMessageBubbleLabelHorizontalPadding, ATLMessageBubbleLabelVerticalPadding, self.bubbleView.frame.size.width-(ATLMessageBubbleLabelVerticalPadding*2), self.bubbleView.frame.size.height-(ATLMessageBubbleLabelVerticalPadding*2));
}
- (NSString *)hexStringFromColor:(UIColor *)color {
CGColorSpaceModel colorSpace = CGColorSpaceGetModel(CGColorGetColorSpace(color.CGColor));
const CGFloat *components = CGColorGetComponents(color.CGColor);
CGFloat r, g, b, a;
if (colorSpace == kCGColorSpaceModelMonochrome) {
r = components[0];
g = components[0];
b = components[0];
a = components[1];
}
else if (colorSpace == kCGColorSpaceModelRGB) {
r = components[0];
g = components[1];
b = components[2];
a = components[3];
}
return [NSString stringWithFormat:@"#%02lX%02lX%02lX",
lroundf(r * 255),
lroundf(g * 255),
lroundf(b * 255)];
}
@end