NewsBlur/media/ios/Classes/InteractionCell.m

127 lines
5.1 KiB
Mathematica
Raw Normal View History

//
// InteractionCell.m
// NewsBlur
//
// Created by Roy Yang on 7/16/12.
// Copyright (c) 2012 NewsBlur. All rights reserved.
//
#import "InteractionCell.h"
#import "NSAttributedString+Attributes.h"
#import "UIImageView+AFNetworking.h"
#import "Utilities.h"
2012-07-18 17:23:57 -07:00
#import <QuartzCore/QuartzCore.h>
@implementation InteractionCell
@synthesize interactionLabel;
@synthesize avatarView;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
interactionLabel = nil;
avatarView = nil;
// create the label and the avatar
UIImageView *avatar = [[UIImageView alloc] initWithFrame:CGRectZero];
self.avatarView = avatar;
[self.contentView addSubview:avatar];
OHAttributedLabel *interaction = [[OHAttributedLabel alloc] initWithFrame:CGRectZero];
interaction.backgroundColor = [UIColor whiteColor];
interaction.automaticallyAddLinksForType = NO;
self.interactionLabel = interaction;
[self.contentView addSubview:interaction];
}
return self;
}
- (void)layoutSubviews {
[super layoutSubviews];
// determine outer bounds
CGRect contentRect = self.contentView.bounds;
2012-07-18 17:23:57 -07:00
// position avatar to bounds
self.avatarView.frame = CGRectMake(20, 15, 48, 48);
// position label to bounds
CGRect labelRect = contentRect;
labelRect.origin.x = labelRect.origin.x + 83;
labelRect.origin.y = labelRect.origin.y + 14;
labelRect.size.width = contentRect.size.width - 83 - 20;
self.interactionLabel.frame = labelRect;
}
- (int)setInteraction:(NSDictionary *)interaction withWidth:(int)width {
// must set the height again for dynamic height in heightForRowAtIndexPath in
CGRect interactionLabelRect = self.interactionLabel.bounds;
interactionLabelRect.size.width = width - 83 - 40;
self.interactionLabel.frame = interactionLabelRect;
UIImage *placeholder = [UIImage imageNamed:@"user"];
[self.avatarView setImageWithURL:[NSURL URLWithString:[[interaction objectForKey:@"with_user"] objectForKey:@"photo_url"]]
placeholderImage:placeholder];
NSString *category = [interaction objectForKey:@"category"];
NSString *content = [interaction objectForKey:@"content"];
NSString *title = [self stripFormatting:[NSString stringWithFormat:@"%@", [interaction objectForKey:@"title"]]];
NSString *username = [[interaction objectForKey:@"with_user"] objectForKey:@"username"];
NSString *time = [[NSString stringWithFormat:@"%@ ago", [interaction objectForKey:@"time_since"]] uppercaseString];
NSString *comment = [NSString stringWithFormat:@"\"%@\"", content];
NSString *txt;
if ([category isEqualToString:@"follow"]) {
txt = [NSString stringWithFormat:@"%@ is now following you.", username];
} else if ([category isEqualToString:@"comment_reply"]) {
txt = [NSString stringWithFormat:@"%@ replied to your comment:\n%@", username, comment];
} else if ([category isEqualToString:@"reply_reply"]) {
txt = [NSString stringWithFormat:@"%@ replied to your reply:\n%@", username, comment];
} else if ([category isEqualToString:@"story_reshare"]) {
if ([content isEqualToString:@""] || content == nil) {
txt = [NSString stringWithFormat:@"%@ re-shared %@.", username, title];
} else {
txt = [NSString stringWithFormat:@"%@ re-shared %@:\n%@", username, title, comment];
}
}
NSString *txtWithTime = [NSString stringWithFormat:@"%@\n%@", txt, time];
NSMutableAttributedString* attrStr = [NSMutableAttributedString attributedStringWithString:txtWithTime];
[attrStr setFont:[UIFont fontWithName:@"Helvetica" size:14]];
[attrStr setTextColor:UIColorFromRGB(0x333333)];
if (![username isEqualToString:@"You"]){
[attrStr setTextColor:UIColorFromRGB(NEWSBLUR_ORANGE) range:[txtWithTime rangeOfString:username]];
[attrStr setTextBold:YES range:[txt rangeOfString:username]];
}
[attrStr setTextColor:UIColorFromRGB(NEWSBLUR_ORANGE) range:[txtWithTime rangeOfString:title]];
[attrStr setTextColor:UIColorFromRGB(0x666666) range:[txtWithTime rangeOfString:comment]];
[attrStr setTextColor:UIColorFromRGB(0x999999) range:[txtWithTime rangeOfString:time]];
[attrStr setFont:[UIFont fontWithName:@"Helvetica" size:10] range:[txtWithTime rangeOfString:time]];
2012-07-18 17:23:57 -07:00
[attrStr setTextAlignment:kCTLeftTextAlignment lineBreakMode:kCTLineBreakByWordWrapping lineHeight:4];
self.interactionLabel.attributedText = attrStr;
[self.interactionLabel sizeToFit];
int height = self.interactionLabel.frame.size.height;
return height;
}
- (NSString *)stripFormatting:(NSString *)str {
while ([str rangeOfString:@" "].location != NSNotFound) {
str = [str stringByReplacingOccurrencesOfString:@" " withString:@" "];
}
while ([str rangeOfString:@"\n"].location != NSNotFound) {
str = [str stringByReplacingOccurrencesOfString:@"\n" withString:@" "];
}
return str;
}
@end