// // MessageCell.m // Juick // // Created by Vitaly Takmazov on 29.10.13. // Copyright (c) 2013 com.juick. All rights reserved. // #import "MessageCell.h" #import "ColorScheme.h" #import "NSDate+TimeAgo.h" #import "UIImage+Utils.h" #define kAvatarSizeX 32 #define kAvatarSizeY 32 #define kPaddingX 10 #define kPaddingY 10 @implementation MessageCell - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; if (self) { [self.contentView setBackgroundColor:[UIColor whiteColor]]; self.avatar = [[UIImageView alloc] initWithFrame:CGRectZero]; [self.avatar setBackgroundColor:[UIColor clearColor]]; self.titleLabel = [[UILabel alloc] initWithFrame:CGRectZero]; [self.titleLabel setLineBreakMode:NSLineBreakByTruncatingTail]; [self.titleLabel setNumberOfLines:1]; [self.titleLabel setTextAlignment:NSTextAlignmentLeft]; [self.titleLabel setTextColor:[ColorScheme linkColor]]; [self.titleLabel setBackgroundColor:[UIColor clearColor]]; self.timestampLabel = [[UILabel alloc] initWithFrame:CGRectZero]; [self.timestampLabel setLineBreakMode:NSLineBreakByTruncatingTail]; [self.timestampLabel setNumberOfLines:1]; [self.timestampLabel setTextAlignment:NSTextAlignmentLeft]; [self.timestampLabel setTextColor:[UIColor grayColor]]; [self.timestampLabel setBackgroundColor:[UIColor clearColor]]; self.bodyLabel = [[UILabel alloc] initWithFrame:CGRectZero]; //self.bodyLabel.dataDetectorTypes = NSTextCheckingTypeLink; //self.bodyLabel.delegate = self; [self.bodyLabel setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisVertical]; [self.bodyLabel setLineBreakMode:NSLineBreakByTruncatingTail]; [self.bodyLabel setNumberOfLines:0]; [self.bodyLabel setTextAlignment:NSTextAlignmentLeft]; [self.bodyLabel setTextColor:[UIColor darkGrayColor]]; [self.bodyLabel setBackgroundColor:[UIColor clearColor]]; self.summaryLabel = [[UILabel alloc] initWithFrame:CGRectZero]; [self.summaryLabel setLineBreakMode:NSLineBreakByTruncatingTail]; [self.summaryLabel setNumberOfLines:1]; [self.summaryLabel setTextAlignment:NSTextAlignmentLeft]; [self.summaryLabel setTextColor:[UIColor grayColor]]; [self.summaryLabel setBackgroundColor:[UIColor clearColor]]; self.attach = [[UIImageView alloc] initWithFrame:CGRectZero]; [self.attach setBackgroundColor:[UIColor clearColor]]; self.attach.contentMode = UIViewContentModeTopLeft; [self.contentView addSubview:self.avatar]; [self.contentView addSubview:self.titleLabel]; [self.contentView addSubview:self.timestampLabel]; [self.contentView addSubview:self.bodyLabel]; [self.contentView addSubview:self.attach]; [self.contentView addSubview:self.summaryLabel]; [self updateFonts]; } return self; } - (void)setSelected:(BOOL)selected animated:(BOOL)animated { [super setSelected:selected animated:animated]; // Configure the view for the selected state } - (CGRect) avatarRect { return CGRectMake(kPaddingX, kPaddingY, kAvatarSizeX, kAvatarSizeY); } - (CGRect) bodyRect { CGRect bodySize = [UILabel sizeForLabel:self.bodyLabel width:(self.contentView.bounds.size.width - kPaddingX * 2)]; return CGRectMake(kPaddingX, kPaddingY + kAvatarSizeY + kPaddingY * 2, bodySize.size.width, bodySize.size.height); } - (CGRect) titleRect { CGRect titleSize = [UILabel sizeForLabel:self.titleLabel width:self.contentView.bounds.size.width - [self avatarRect].size.width - kPaddingX * 2]; return CGRectMake(kPaddingX + kAvatarSizeX + kPaddingX, kPaddingY, titleSize.size.width, titleSize.size.height); } - (CGRect) timestampRect { CGRect timeStampSize = [UILabel sizeForLabel:self.timestampLabel width:self.contentView.bounds.size.width - [self avatarRect].size.width - kPaddingX * 2]; return CGRectMake(kPaddingX + [self avatarRect].size.width + kPaddingX, kPaddingY + [self titleRect].size.height + kPaddingY, timeStampSize.size.width, timeStampSize.size.height); } - (CGRect) summaryRect { CGRect summarySize = [UILabel sizeForLabel:self.summaryLabel width:(self.contentView.bounds.size.width - 20)]; float summaryY = 10 + 32 + 10 + [self bodyRect].size.height + 10; if (self.message.attach != nil) { summaryY += [self attachRect].size.height + 10; } return CGRectMake(10, summaryY , summarySize.size.width, summarySize.size.height); } - (CGRect) attachRect { return CGRectMake(10, 10 + 32 + 10 + [self bodyRect].size.height + 10, self.attach.image.size.width, self.attach.image.size.height); } - (CGFloat) heightForCell { CGFloat baseSize = 10 + [self avatarRect].size.height + 20 + [self bodyRect].size.height + 10; if (self.message.repliesCount > 0) { baseSize += 10 + [self summaryRect].size.height; } if (self.attach.image != nil) { baseSize += 10 + [self attachRect].size.height + 10; } return baseSize; } - (void)layoutSubviews { [super layoutSubviews]; self.avatar.frame = [self avatarRect]; self.titleLabel.frame = [self titleRect]; self.timestampLabel.frame = [self timestampRect]; self.bodyLabel.frame = [self bodyRect]; if (self.message.repliesCount > 0) { self.summaryLabel.frame = [self summaryRect]; } if (self.message.attach != nil) { self.attach.frame = [self attachRect]; } } - (void)updateFonts { if (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_6_1) { self.titleLabel.font = [UIFont preferredFontForTextStyle:UIFontTextStyleHeadline]; self.bodyLabel.font = [UIFont preferredFontForTextStyle:UIFontTextStyleBody]; self.timestampLabel.font = [UIFont preferredFontForTextStyle:UIFontTextStyleFootnote]; self.summaryLabel.font = [UIFont preferredFontForTextStyle:UIFontTextStyleFootnote]; } } - (void) setMessage:(Message *)msg { _message = msg; [self.avatar setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://i.juick.com/as/%d.png", [msg.userID intValue]]] placeholderImage:[UIImage imageNamed:@"0.png"] options:SDWebImageContinueInBackground]; self.titleLabel.text = msg.user; NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss"; [formatter setTimeZone:[NSTimeZone timeZoneWithName:@"UTC"]]; self.timestampLabel.text = [[formatter dateFromString:msg.timestamp] timeAgo]; if (msg.repliesCount > 0) { if ([msg.repliesBy length] > 0) { self.summaryLabel.text = [NSString stringWithFormat:@"%@ replies by %@", msg.repliesCount, msg.repliesBy]; } else { self.summaryLabel.text = [NSString stringWithFormat:@"%@ replies", msg.repliesCount]; } } else { self.summaryLabel.text = nil; } self.bodyLabel.text = [msg.text stringByDecodingHTMLEntities]; if ([msg.tags count] > 0) { self.bodyLabel.text = [[NSString alloc] initWithFormat:@"%@\n%@", [msg.tags componentsJoinedByString:@", "], [msg.text stringByDecodingHTMLEntities]]; } [self setNeedsLayout]; } - (UITableView *) getTable { id view = [self superview]; while ([view isKindOfClass:[UITableView class]] == NO) { view = [view superview]; } return (UITableView *)view; } - (void) attributedLabel:(TTTAttributedLabel *)label didSelectLinkWithURL:(NSURL *)url { [[UIApplication sharedApplication] openURL:url]; } @end