From 8a79dcc214d525d1a59ee95173583976cd774287 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Sun, 14 Aug 2016 02:21:31 +0300 Subject: new message cell layout --- Juick/Juick-Prefix.pch | 8 +- Juick/LoginViewController.m | 3 - Juick/MessageCell.h | 44 ---------- Juick/MessageCell.m | 190 ----------------------------------------- Juick/MessagesViewController.m | 46 ++-------- Juick/Views/MessageCell.h | 21 +++++ Juick/Views/MessageCell.m | 49 +++++++++++ Juick/Views/MessageCell.xib | 85 ++++++++++++++++++ 8 files changed, 166 insertions(+), 280 deletions(-) delete mode 100644 Juick/MessageCell.h delete mode 100644 Juick/MessageCell.m create mode 100644 Juick/Views/MessageCell.h create mode 100644 Juick/Views/MessageCell.m create mode 100644 Juick/Views/MessageCell.xib (limited to 'Juick') diff --git a/Juick/Juick-Prefix.pch b/Juick/Juick-Prefix.pch index ad49a41..a0abcee 100644 --- a/Juick/Juick-Prefix.pch +++ b/Juick/Juick-Prefix.pch @@ -15,6 +15,10 @@ #import #import #import - #define MAS_SHORTHAND - #import "Masonry.h" + #import + #import "NSString+HTML.h" + #import "UIImage+Utils.h" + #import "UILabel+Utils.h" + #import "NSDate+TimeAgo.h" + #import "TTTAttributedLabel.h" #endif diff --git a/Juick/LoginViewController.m b/Juick/LoginViewController.m index 38082a3..a803c48 100644 --- a/Juick/LoginViewController.m +++ b/Juick/LoginViewController.m @@ -93,9 +93,6 @@ static NSString *CellIdentifier = @"CellIdentifier"; textField.text = [[PDKeychainBindings sharedKeychainBindings] stringForKey:@"com.juick.password"]; } [cell.contentView addSubview:textField]; - [textField makeConstraints:^(MASConstraintMaker *make) { - make.edges.equalTo(cell.contentView).with.insets(UIEdgeInsetsMake(5, 25, 5, 25)); - }]; return cell; } diff --git a/Juick/MessageCell.h b/Juick/MessageCell.h deleted file mode 100644 index 6140e2d..0000000 --- a/Juick/MessageCell.h +++ /dev/null @@ -1,44 +0,0 @@ -// -// MessageCell.h -// Juick -// -// Created by Vitaly Takmazov on 29.10.13. -// Copyright (c) 2013 com.juick. All rights reserved. -// - -#import -#import -#import "TTTAttributedLabel.h" - -#import "Message.h" - -#import "NSString+HTML.h" -#import "UIImage+Utils.h" -#import "UILabel+Utils.h" - - -#define kLabelHorizontalInsets 20.0f - -@interface MessageCell : UITableViewCell - -@property (strong, nonatomic) IBOutlet Message *message; -@property (strong, nonatomic) IBOutlet UILabel *titleLabel; -@property (strong, nonatomic) IBOutlet UILabel *timestampLabel; -@property (strong, nonatomic) IBOutlet UIImageView *avatar; -@property (strong, nonatomic) IBOutlet UILabel *bodyLabel; -@property (strong, nonatomic) IBOutlet UIImageView *attach; -@property (strong, nonatomic) IBOutlet UILabel *summaryLabel; - -- (void) updateFonts; -- (CGRect) avatarRect; -- (CGRect) timestampRect; -- (CGRect) titleRect; -- (CGRect) bodyRect; -- (CGRect) attachRect; -- (CGRect) summaryRect; - -- (CGFloat) heightForCell; - -- (UITableView *) getTable; - -@end diff --git a/Juick/MessageCell.m b/Juick/MessageCell.m deleted file mode 100644 index 9994e78..0000000 --- a/Juick/MessageCell.m +++ /dev/null @@ -1,190 +0,0 @@ -// -// 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:@"https://i.juick.com/a/%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 diff --git a/Juick/MessagesViewController.m b/Juick/MessagesViewController.m index ff7e556..2cf4267 100644 --- a/Juick/MessagesViewController.m +++ b/Juick/MessagesViewController.m @@ -21,9 +21,6 @@ static NSString *CellIdentifier = @"MessageCell"; -static NSMutableDictionary *heightsCache; - - @interface MessagesViewController (); @@ -84,8 +81,9 @@ static NSMutableDictionary *heightsCache; self.navigationController.navigationBar.tintColor = [ColorScheme navbarBackground]; } - [self.tableView registerClass:[MessageCell class] forCellReuseIdentifier:CellIdentifier]; - heightsCache = [[NSMutableDictionary alloc] init]; + [self.tableView registerNib:[UINib nibWithNibName:@"MessageCell" bundle:nil] forCellReuseIdentifier:CellIdentifier]; + self.tableView.rowHeight = UITableViewAutomaticDimension; + self.tableView.estimatedRowHeight = 500.0f; } - (void) composePressed { @@ -119,12 +117,6 @@ static NSMutableDictionary *heightsCache; } } -- (void)didReceiveMemoryWarning -{ - [super didReceiveMemoryWarning]; - // Dispose of any resources that can be recreated. -} - - (void)contentSizeCategoryChanged:(NSNotification *)notification { [self.tableView reloadData]; @@ -139,8 +131,7 @@ static NSMutableDictionary *heightsCache; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { - MessageCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; - [cell updateFonts]; + MessageCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; Message *msg = [_messages objectAtIndex:indexPath.row]; [cell setMessage:msg]; if ([msg.attach length] > 0) { @@ -148,12 +139,7 @@ static NSMutableDictionary *heightsCache; [manager downloadWithURL:[NSURL URLWithString:msg.attach] options:SDWebImageContinueInBackground progress:^(NSInteger receivedSize, NSInteger expectedSize) { // <#code#> } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished) { - [cell.attach setImage:[UIImage imageWithImage:image convertToWidth:300.0f]]; - [heightsCache setObject:[NSNumber numberWithFloat:[cell heightForCell]] forKey:[NSString stringWithFormat:@"%@#%@", msg.MID, msg.RID]]; - [tableView beginUpdates]; - [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone]; - [tableView endUpdates]; - + [cell.attach setImage:[UIImage imageWithImage:image convertToWidth:300.0f]]; }]; } else { cell.attach.image = nil; @@ -162,28 +148,6 @@ static NSMutableDictionary *heightsCache; return cell; } -- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath -{ - Message *msg = [_messages objectAtIndex:indexPath.row]; - float height = [[heightsCache objectForKey:[NSString stringWithFormat:@"%@#%@", msg.MID, msg.RID]] floatValue]; - if (height > 0) { - return height; - } else { - MessageCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; - [cell updateFonts]; - [cell setMessage:msg]; - return [cell heightForCell]; - } -} - -- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath -{ - /* if (self.dataLoading) { - return [self tableView:tableView heightForRowAtIndexPath:indexPath]; - }*/ - return 500.0f; -} - - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { if ([self.path isEqualToString:[Message threadUrl]]) return; diff --git a/Juick/Views/MessageCell.h b/Juick/Views/MessageCell.h new file mode 100644 index 0000000..c40a12c --- /dev/null +++ b/Juick/Views/MessageCell.h @@ -0,0 +1,21 @@ +// +// MessageCell.h +// Juick +// +// Created by Vitaly Takmazov on 14/08/16. +// Copyright © 2016 com.juick. All rights reserved. +// + +#import +#import "Message.h" + +@interface MessageCell : UITableViewCell + +@property (weak, nonatomic) IBOutlet UILabel *title; +@property (weak, nonatomic) IBOutlet UILabel *timestamp; +@property (weak, nonatomic) IBOutlet UIImageView *avatar; +@property (weak, nonatomic) IBOutlet UILabel *text; +@property (weak, nonatomic) IBOutlet UIImageView *attach; +@property (weak, nonatomic) IBOutlet UILabel *summary; +@property (strong, nonatomic) Message * message; +@end diff --git a/Juick/Views/MessageCell.m b/Juick/Views/MessageCell.m new file mode 100644 index 0000000..8759e63 --- /dev/null +++ b/Juick/Views/MessageCell.m @@ -0,0 +1,49 @@ +// +// MessageCell.m +// Juick +// +// Created by Vitaly Takmazov on 14/08/16. +// Copyright © 2016 com.juick. All rights reserved. +// + +#import "MessageCell.h" + +@implementation MessageCell + +- (void)awakeFromNib { + [super awakeFromNib]; + // Initialization code +} + +- (void)setSelected:(BOOL)selected animated:(BOOL)animated { + [super setSelected:selected animated:animated]; + + // Configure the view for the selected state +} + +- (void) setMessage:(Message *)msg { + _message = msg; + [self.avatar setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"https://i.juick.com/a/%d.png", [msg.userID intValue]]] placeholderImage:[UIImage imageNamed:@"0.png"] options:SDWebImageContinueInBackground]; + self.title.text = msg.user; + NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; + formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss"; + [formatter setTimeZone:[NSTimeZone timeZoneWithName:@"UTC"]]; + + self.timestamp.text = [[formatter dateFromString:msg.timestamp] timeAgo]; + if (msg.repliesCount > 0) { + if ([msg.repliesBy length] > 0) { + self.summary.text = [NSString stringWithFormat:@"%@ replies by %@", msg.repliesCount, msg.repliesBy]; + } else { + self.summary.text = [NSString stringWithFormat:@"%@ replies", msg.repliesCount]; + } + } else { + self.summary.text = nil; + } + self.text.text = [msg.text stringByDecodingHTMLEntities]; + if ([msg.tags count] > 0) { + self.text.text = [[NSString alloc] initWithFormat:@"%@\n%@", [msg.tags componentsJoinedByString:@", "], + [msg.text stringByDecodingHTMLEntities]]; + } +} + +@end diff --git a/Juick/Views/MessageCell.xib b/Juick/Views/MessageCell.xib new file mode 100644 index 0000000..29f37fb --- /dev/null +++ b/Juick/Views/MessageCell.xib @@ -0,0 +1,85 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + -- cgit v1.2.3