summaryrefslogtreecommitdiff
path: root/Juick/MessageCell.m
diff options
context:
space:
mode:
Diffstat (limited to 'Juick/MessageCell.m')
-rw-r--r--Juick/MessageCell.m158
1 files changed, 82 insertions, 76 deletions
diff --git a/Juick/MessageCell.m b/Juick/MessageCell.m
index d396181..8c2f001 100644
--- a/Juick/MessageCell.m
+++ b/Juick/MessageCell.m
@@ -8,14 +8,13 @@
#import "MessageCell.h"
#import "ColorScheme.h"
-#import <SDWebImage/UIImageView+WebCache.h>
#import "NSDate+TimeAgo.h"
+#import "UIImage+Utils.h"
-@interface MessageCell ()
-
-@property (nonatomic, assign) BOOL didSetupConstraints;
-
-@end
+#define kAvatarSizeX 32
+#define kAvatarSizeY 32
+#define kPaddingX 10
+#define kPaddingY 10
@implementation MessageCell
@@ -24,8 +23,7 @@
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
[self.contentView setBackgroundColor:[UIColor whiteColor]];
- self.contentView.bounds = CGRectMake(0, 0, 320, 999);
- self.avatar = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 32, 32)];
+ self.avatar = [[UIImageView alloc] initWithFrame:CGRectZero];
[self.avatar setBackgroundColor:[UIColor clearColor]];
self.titleLabel = [[UILabel alloc] initWithFrame:CGRectZero];
@@ -42,9 +40,9 @@
[self.timestampLabel setTextColor:[UIColor grayColor]];
[self.timestampLabel setBackgroundColor:[UIColor clearColor]];
- self.bodyLabel = [[TTTAttributedLabel alloc] initWithFrame:CGRectZero];
- self.bodyLabel.dataDetectorTypes = NSTextCheckingTypeLink;
- self.bodyLabel.delegate = self;
+ 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];
@@ -53,16 +51,15 @@
[self.bodyLabel setBackgroundColor:[UIColor clearColor]];
- self.summaryLabel = [[TTTAttributedLabel alloc] initWithFrame:CGRectZero];
- [self.summaryLabel setTranslatesAutoresizingMaskIntoConstraints:NO];
+ 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 = [[UIImageView alloc]initWithImage:[UIImage imageWithImage:[UIImage imageNamed:@"AttachPlaceholder.png"] convertToWidth:300.0f]];
[self.attach setBackgroundColor:[UIColor clearColor]];
- self.attach.contentMode = UIViewContentModeScaleAspectFit;
+ self.attach.contentMode = UIViewContentModeTopLeft;
[self.contentView addSubview:self.avatar];
[self.contentView addSubview:self.titleLabel];
@@ -83,53 +80,65 @@
// Configure the view for the selected state
}
-- (void)updateConstraints {
- [super updateConstraints];
-
- if (self.didSetupConstraints) return;
-
- UIEdgeInsets padding = UIEdgeInsetsMake(10, 20, 10, 20);
-
- [self.avatar makeConstraints:^(MASConstraintMaker *make) {
- make.top.equalTo(self.contentView.top).with.offset(padding.top);
- make.left.equalTo(self.contentView.left).with.offset(padding.left);
- make.width.equalTo(@32);
- make.height.equalTo(@32);
- }];
-
- [self.titleLabel makeConstraints:^(MASConstraintMaker *make) {
- make.left.equalTo(self.avatar.right).with.offset(padding.right);
- make.top.equalTo(self.contentView.top).with.offset(padding.top);
- make.right.equalTo(self.contentView.right).with.offset(-padding.right);
- }];
- [self.timestampLabel makeConstraints:^(MASConstraintMaker *make) {
- make.left.equalTo(self.avatar.right).with.offset(padding.left);
- make.top.equalTo(self.titleLabel.bottom).with.offset(padding.top /2);
- make.right.equalTo(self.contentView.right).with.offset(-padding.right);
- }];
-
- [self.bodyLabel makeConstraints:^(MASConstraintMaker *make) {
- make.left.equalTo(self.contentView.left).with.offset(padding.left);
- make.top.equalTo(self.timestampLabel.bottom).with.offset(padding.top);
- make.right.equalTo(self.contentView.right).with.offset(-padding.right);
- }];
-
- [self.attach makeConstraints:^(MASConstraintMaker *make) {
- make.left.equalTo(self.contentView.left).with.offset(padding.left);
- make.right.equalTo(self.contentView.right).with.offset(-padding.right);
- make.top.equalTo(self.bodyLabel.bottom).with.offset(padding.top);
- }];
-
- [self.summaryLabel makeConstraints:^(MASConstraintMaker *make) {
- make.left.equalTo(self.contentView.left).with.offset(padding.left);
- make.bottom.equalTo(self.contentView.bottom).with.offset(-padding.bottom);
- make.right.equalTo(self.contentView.right).with.offset(-padding.right);
- make.top.equalTo(self.attach.bottom).with.offset(padding.top);
- }];
-
-
-
- self.didSetupConstraints = YES;
+- (CGRect) sizeForLabel:(UILabel *)label width:(CGFloat)width {
+ return [label.text boundingRectWithSize:(CGSize) {width, CGFLOAT_MAX} options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:label.font} context:nil];
+}
+
+- (CGRect) avatarRect {
+ return CGRectMake(kPaddingX, kPaddingY, kAvatarSizeX, kAvatarSizeY);
+}
+
+- (CGRect) bodyRect {
+ CGRect bodySize = [self 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 = [self 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 = [self 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 = [self 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];
+ }
}
@@ -143,8 +152,9 @@
}
}
-- (void) bind:(Message *)msg {
- [self.avatar setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://i.juick.com/as/%d.png", [msg.userID intValue]]]];
+- (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];
__weak MessageCell *weakSelf = self;
self.titleLabel.text = msg.user;
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
@@ -167,22 +177,18 @@
[msg.text stringByDecodingHTMLEntities]];
}
if ([msg.attach length] > 0) {
- [self.attach setImageWithURL:[NSURL URLWithString:msg.attach] placeholderImage:[UIImage imageNamed:@"AttachPlaceholder.png"] options:SDWebImageContinueInBackground
- progress:^(NSUInteger receivedSize, long long expectedSize) {
- // <#code#>
- } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType) {
- [weakSelf setNeedsUpdateConstraints];
- [weakSelf updateConstraintsIfNeeded];
- [weakSelf.contentView setNeedsLayout];
- [weakSelf.contentView layoutIfNeeded];
+ SDWebImageManager * manager = [SDWebImageManager sharedManager];
+ [manager downloadWithURL:[NSURL URLWithString:msg.attach] options:SDWebImageContinueInBackground progress:^(NSUInteger receivedSize, long long expectedSize) {
+ // <#code#>
+ } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished) {
+ self.attach.image = [UIImage imageWithImage:image convertToWidth:300.0f];
+ [self setNeedsLayout];
+ [self layoutIfNeeded];
}];
} else {
self.attach.image = nil;
}
- [weakSelf setNeedsUpdateConstraints];
- [weakSelf updateConstraintsIfNeeded];
- [weakSelf.contentView setNeedsLayout];
- [weakSelf.contentView layoutIfNeeded];
+ [self setNeedsLayout];
}
- (void) attributedLabel:(TTTAttributedLabel *)label didSelectLinkWithURL:(NSURL *)url {