Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // MEOutgoingMessageCollectionViewCell.m
- #import "MEOutgoingMessageCollectionViewCell.h"
- #import "METextInputView.h"
- #import "UIFont+Fonts.h"
- @implementation MEOutgoingMessageCollectionViewCell
- + (NSString *)reuseIdentifier {
- return NSStringFromClass([self class]);
- }
- - (id)initWithFrame:(CGRect)frame {
- self = [super initWithFrame:frame];
- if (self) {
- [self me_commonInit];
- }
- return self;
- }
- - (id)initWithCoder:(NSCoder *)aDecoder {
- self = [super initWithCoder:aDecoder];
- if (self) {
- [self me_commonInit];
- }
- return self;
- }
- - (void)me_commonInit {
- self.bubbleView = [[UIView alloc] initWithFrame:CGRectZero];
- self.bubbleView.layer.cornerRadius = [[ATLMessageCollectionViewCell appearance] bubbleViewCornerRadius];
- [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:ATLMIMETypeTextPlain]) {
- NSString *messageString = [[NSString alloc] initWithData:part.data encoding:NSUTF8StringEncoding];
- // UIColor *messageTextColor = [[ATLOutgoingMessageCollectionViewCell appearance] messageTextColor];
- #warning use Gotham-Book 14 once ME-SDK can access the custom font
- NSString *messageHTML = [METextInputView convertSubstituedToHTML:messageString withFont:[UIFont fontWithName:@"Helvetica" size:14] textColor:[UIColor colorWithHex:@"FFFFFF"]];
- [self setHTMLString:messageHTML];
- messageString = nil;
- }
- }
- - (void)layoutSubviews {
- [super layoutSubviews];
- CGFloat avatarImageViewDiameter = [[ATLAvatarImageView appearance] avatarImageViewDiameter];
- self.avatarImageView.frame = CGRectMake(self.contentView.frame.size.width - avatarImageViewDiameter - ATLMessageBubbleLabelHorizontalPadding, ATLMessageBubbleLabelVerticalPadding, avatarImageViewDiameter, avatarImageViewDiameter);
- 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
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement