From 08574251153406e4755f58c2ee5548ee32e308fb Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Sun, 3 Nov 2013 06:08:35 +0400 Subject: avatars --- Juick/Juick-Prefix.pch | 1 + Juick/MasterViewController.m | 3 ++ Juick/Message.h | 1 + Juick/Message.m | 1 + Juick/MessageCell.h | 3 +- Juick/MessageCell.m | 86 +++++++++++++------------------------------- 6 files changed, 32 insertions(+), 63 deletions(-) (limited to 'Juick') diff --git a/Juick/Juick-Prefix.pch b/Juick/Juick-Prefix.pch index 82a2bb4..3db364c 100644 --- a/Juick/Juick-Prefix.pch +++ b/Juick/Juick-Prefix.pch @@ -13,4 +13,5 @@ #ifdef __OBJC__ #import #import + #define MAS_SHORTHAND #endif diff --git a/Juick/MasterViewController.m b/Juick/MasterViewController.m index 26035cd..4ed8df1 100644 --- a/Juick/MasterViewController.m +++ b/Juick/MasterViewController.m @@ -127,6 +127,7 @@ static NSString *CellIdentifier = @"MessageCell"; MessageCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; [cell updateFonts]; Message *msg = [_messages objectAtIndex:indexPath.row]; + [cell.avatar setImage:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://i.juick.com/as/%@.png", msg.userID]]]]]; cell.titleLabel.text = msg.user; cell.bodyLabel.text = msg.text; [cell setNeedsUpdateConstraints]; @@ -136,11 +137,13 @@ static NSString *CellIdentifier = @"MessageCell"; - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { + MessageCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; [cell updateFonts]; Message *msg = [_messages objectAtIndex:indexPath.row]; + [cell.avatar setImage:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://i.juick.com/as/%@.png", msg.userID]]]]]; cell.titleLabel.text = msg.user; cell.bodyLabel.text = msg.text; diff --git a/Juick/Message.h b/Juick/Message.h index 071d318..5ec3732 100644 --- a/Juick/Message.h +++ b/Juick/Message.h @@ -14,6 +14,7 @@ @property(nonatomic, copy) NSDecimalNumber *MID; @property(nonatomic, copy) NSDecimalNumber *RID; @property(nonatomic, copy) NSString *user; +@property(nonatomic, copy) NSDecimalNumber *userID; @property(nonatomic, copy) NSString *text; -(id) initWithDictionary:(NSDictionary *)dictionary; diff --git a/Juick/Message.m b/Juick/Message.m index 08f9289..2c35ef4 100644 --- a/Juick/Message.m +++ b/Juick/Message.m @@ -18,6 +18,7 @@ NSString *text = [[dictionary objectForKey:@"body"] stringByDecodingHTMLEntities]; NSDictionary *userDict = [dictionary objectForKey:@"user"]; result.user = [userDict objectForKey:@"uname"]; + result.userID = [userDict objectForKey:@"uid"]; NSArray *tagsArray = [dictionary objectForKey:@"tags"]; if ([tagsArray count] > 0) { result.text = [[NSString alloc] initWithFormat:@"%@\n%@", [tagsArray componentsJoinedByString:@", "], text]; diff --git a/Juick/MessageCell.h b/Juick/MessageCell.h index 299c3b3..f6b7cc3 100644 --- a/Juick/MessageCell.h +++ b/Juick/MessageCell.h @@ -14,7 +14,8 @@ @interface MessageCell : UITableViewCell -@property (strong, nonatomic) IBOutlet TTTAttributedLabel *titleLabel; +@property (strong, nonatomic) IBOutlet UILabel *titleLabel; +@property (strong, nonatomic) IBOutlet UIImageView *avatar; @property (strong, nonatomic) IBOutlet TTTAttributedLabel *bodyLabel; - (void)updateFonts; diff --git a/Juick/MessageCell.m b/Juick/MessageCell.m index dc698b3..f20acbe 100644 --- a/Juick/MessageCell.m +++ b/Juick/MessageCell.m @@ -7,6 +7,7 @@ // #import "MessageCell.h" +#import "Masonry.h" @interface MessageCell () @@ -20,7 +21,10 @@ { self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; if (self) { - self.titleLabel = [[TTTAttributedLabel alloc] initWithFrame:CGRectZero]; + self.avatar = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 32, 32)]; + [self.avatar setBackgroundColor:[UIColor clearColor]]; + + self.titleLabel = [[UILabel alloc] initWithFrame:CGRectZero]; [self.titleLabel setTranslatesAutoresizingMaskIntoConstraints:NO]; [self.titleLabel setLineBreakMode:NSLineBreakByTruncatingTail]; [self.titleLabel setNumberOfLines:1]; @@ -38,6 +42,7 @@ [self.bodyLabel setTextAlignment:NSTextAlignmentLeft]; [self.bodyLabel setTextColor:[UIColor darkGrayColor]]; [self.bodyLabel setBackgroundColor:[UIColor clearColor]]; + [self.contentView addSubview:self.avatar]; [self.contentView addSubview:self.titleLabel]; [self.contentView addSubview:self.bodyLabel]; @@ -58,70 +63,27 @@ if (self.didSetupConstraints) return; - [self.contentView addConstraint:[NSLayoutConstraint - constraintWithItem:self.titleLabel - attribute:NSLayoutAttributeLeading - relatedBy:NSLayoutRelationEqual - toItem:self.contentView - attribute:NSLayoutAttributeLeading - multiplier:1.0f - constant:kLabelHorizontalInsets]]; - - [self.contentView addConstraint:[NSLayoutConstraint - constraintWithItem:self.titleLabel - attribute:NSLayoutAttributeTop - relatedBy:NSLayoutRelationEqual - toItem:self.contentView - attribute:NSLayoutAttributeTop - multiplier:1.0f - constant:(kLabelHorizontalInsets / 2)]]; - - [self.contentView addConstraint:[NSLayoutConstraint - constraintWithItem:self.titleLabel - attribute:NSLayoutAttributeTrailing - relatedBy:NSLayoutRelationEqual - toItem:self.contentView - attribute:NSLayoutAttributeTrailing - multiplier:1.0f - constant:-kLabelHorizontalInsets]]; - - ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// - - [self.contentView addConstraint:[NSLayoutConstraint - constraintWithItem:self.bodyLabel - attribute:NSLayoutAttributeLeading - relatedBy:NSLayoutRelationEqual - toItem:self.contentView - attribute:NSLayoutAttributeLeading - multiplier:1.0f - constant:kLabelHorizontalInsets]]; + UIEdgeInsets padding = UIEdgeInsetsMake(10, 20, 10, 20); - [self.contentView addConstraint:[NSLayoutConstraint - constraintWithItem:self.bodyLabel - attribute:NSLayoutAttributeTop - relatedBy:NSLayoutRelationEqual - toItem:self.titleLabel - attribute:NSLayoutAttributeBottom - multiplier:1.0f - constant:(kLabelHorizontalInsets / 4)]]; + [self.avatar makeConstraints:^(MASConstraintMaker *make) { + make.top.equalTo(self.contentView.top).with.offset(padding.top); + make.leading.equalTo(self.contentView.leading).with.offset(padding.left); + make.width.lessThanOrEqualTo(@32); + make.height.lessThanOrEqualTo(@32); + }]; - [self.contentView addConstraint:[NSLayoutConstraint - constraintWithItem:self.bodyLabel - attribute:NSLayoutAttributeTrailing - relatedBy:NSLayoutRelationEqual - toItem:self.contentView - attribute:NSLayoutAttributeTrailing - multiplier:1.0f - constant:-kLabelHorizontalInsets]]; + [self.titleLabel makeConstraints:^(MASConstraintMaker *make) { + make.leading.equalTo(self.avatar.trailing).with.offset(padding.right); + make.top.equalTo(self.contentView.top).with.offset(padding.top); + make.trailing.equalTo(self.contentView.trailing).with.offset(-padding.right); + }]; - [self.contentView addConstraint:[NSLayoutConstraint - constraintWithItem:self.bodyLabel - attribute:NSLayoutAttributeBottom - relatedBy:NSLayoutRelationEqual - toItem:self.contentView - attribute:NSLayoutAttributeBottom - multiplier:1.0f - constant:-(kLabelHorizontalInsets / 2)]]; + [self.bodyLabel makeConstraints:^(MASConstraintMaker *make) { + make.leading.equalTo(self.contentView.leading).with.offset(padding.left); + make.top.equalTo(self.avatar.bottom).with.offset(padding.top); + make.trailing.equalTo(self.contentView.trailing).with.offset(-padding.right); + make.bottom.equalTo(self.contentView.bottom).with.offset(-padding.bottom); + }]; self.didSetupConstraints = YES; } -- cgit v1.2.3