Advertisement
Guest User

Untitled

a guest
Jul 27th, 2016
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.61 KB | None | 0 0
  1. // MEOutgoingMessageCollectionViewCell.m
  2.  
  3. #import "MEOutgoingMessageCollectionViewCell.h"
  4. #import "METextInputView.h"
  5.  
  6. #import "UIFont+Fonts.h"
  7.  
  8. @implementation MEOutgoingMessageCollectionViewCell
  9.  
  10. + (NSString *)reuseIdentifier {
  11. return NSStringFromClass([self class]);
  12. }
  13.  
  14. - (id)initWithFrame:(CGRect)frame {
  15. self = [super initWithFrame:frame];
  16.  
  17. if (self) {
  18. [self me_commonInit];
  19. }
  20.  
  21. return self;
  22. }
  23.  
  24. - (id)initWithCoder:(NSCoder *)aDecoder {
  25. self = [super initWithCoder:aDecoder];
  26.  
  27. if (self) {
  28. [self me_commonInit];
  29. }
  30.  
  31. return self;
  32. }
  33.  
  34. - (void)me_commonInit {
  35. self.bubbleView = [[UIView alloc] initWithFrame:CGRectZero];
  36. self.bubbleView.layer.cornerRadius = [[ATLMessageCollectionViewCell appearance] bubbleViewCornerRadius];
  37. [self.contentView addSubview:self.bubbleView];
  38. [self.contentView sendSubviewToBack:self.bubbleView];
  39.  
  40. self.avatarImageView = [[ATLAvatarImageView alloc] init];
  41. self.avatarImageView.translatesAutoresizingMaskIntoConstraints = NO;
  42. self.avatarImageView.hidden = YES;
  43. self.avatarImageView.frame = CGRectZero;
  44. [self.contentView addSubview:self.avatarImageView];
  45. [self.contentView bringSubviewToFront:self.avatarImageView];
  46.  
  47. }
  48.  
  49. - (void)updateWithSender:(id<ATLParticipant>)sender {
  50. if (sender) {
  51. self.avatarImageView.hidden = NO;
  52. self.avatarImageView.avatarItem = sender;
  53. } else {
  54. self.avatarImageView.hidden = YES;
  55. }
  56. }
  57.  
  58. - (void)shouldDisplayAvatarItem:(BOOL)shouldDisplayAvatarItem {
  59. self.shouldDisplayAvatar = shouldDisplayAvatarItem;
  60. }
  61.  
  62. - (void)presentMessage:(LYRMessage *)message {
  63. self.bubbleView.backgroundColor = [[ATLOutgoingMessageCollectionViewCell appearance] bubbleViewColor];
  64. LYRMessagePart *part = message.parts[0];
  65.  
  66. if ([part.MIMEType isEqualToString:ATLMIMETypeTextPlain]) {
  67. NSString *messageString = [[NSString alloc] initWithData:part.data encoding:NSUTF8StringEncoding];
  68. // UIColor *messageTextColor = [[ATLOutgoingMessageCollectionViewCell appearance] messageTextColor];
  69. #warning use Gotham-Book 14 once ME-SDK can access the custom font
  70. NSString *messageHTML = [METextInputView convertSubstituedToHTML:messageString withFont:[UIFont fontWithName:@"Helvetica" size:14] textColor:[UIColor colorWithHex:@"FFFFFF"]];
  71. [self setHTMLString:messageHTML];
  72. messageString = nil;
  73. }
  74. }
  75.  
  76. - (void)layoutSubviews {
  77. [super layoutSubviews];
  78.  
  79. CGFloat avatarImageViewDiameter = [[ATLAvatarImageView appearance] avatarImageViewDiameter];
  80. self.avatarImageView.frame = CGRectMake(self.contentView.frame.size.width - avatarImageViewDiameter - ATLMessageBubbleLabelHorizontalPadding, ATLMessageBubbleLabelVerticalPadding, avatarImageViewDiameter, avatarImageViewDiameter);
  81.  
  82. if (!self.superview) {
  83. return;
  84. }
  85.  
  86. if (self.shouldDisplayAvatar == NO) { self.avatarImageView.frame = CGRectZero; }
  87.  
  88. CGFloat maxBubbleWidth = ATLMaxCellWidth() + (ATLMessageBubbleLabelHorizontalPadding * 2);
  89. CGFloat textHeight = self.contentView.frame.size.height - (ATLMessageBubbleLabelVerticalPadding * 2);
  90. CGSize textSize = [self.messageView suggestedSizeForTextForSize:CGSizeMake(ATLMaxCellWidth(), textHeight)];
  91. CGFloat bubbleWidth = maxBubbleWidth;
  92. textSize.width += (ATLMessageBubbleLabelHorizontalPadding * 2);
  93. if (textSize.width < maxBubbleWidth) { bubbleWidth = textSize.width; }
  94.  
  95. CGFloat leadIn = self.contentView.frame.size.width - bubbleWidth - ATLMessageCellHorizontalMargin - self.avatarImageView.frame.size.width;
  96.  
  97. self.bubbleView.frame = CGRectMake(leadIn, 0, bubbleWidth, self.contentView.frame.size.height);
  98. self.messageView.frame = CGRectMake(leadIn + ATLMessageBubbleLabelHorizontalPadding, ATLMessageBubbleLabelVerticalPadding, self.bubbleView.frame.size.width - (ATLMessageBubbleLabelVerticalPadding * 2), self.bubbleView.frame.size.height - (ATLMessageBubbleLabelVerticalPadding * 2));
  99. }
  100.  
  101. - (NSString *)hexStringFromColor:(UIColor *)color {
  102. CGColorSpaceModel colorSpace = CGColorSpaceGetModel(CGColorGetColorSpace(color.CGColor));
  103. const CGFloat *components = CGColorGetComponents(color.CGColor);
  104. CGFloat r, g, b, a;
  105. if (colorSpace == kCGColorSpaceModelMonochrome) {
  106. r = components[0];
  107. g = components[0];
  108. b = components[0];
  109. a = components[1];
  110. }
  111. else if (colorSpace == kCGColorSpaceModelRGB) {
  112. r = components[0];
  113. g = components[1];
  114. b = components[2];
  115. a = components[3];
  116. }
  117.  
  118. return [NSString stringWithFormat:@"#%02lX%02lX%02lX",
  119. lroundf(r * 255),
  120. lroundf(g * 255),
  121. lroundf(b * 255)];
  122. }
  123.  
  124. @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement