Created
August 20, 2011 17:52
-
-
Save mrjjwright/1159407 to your computer and use it in GitHub Desktop.
Calculate height for a custom cell
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// NewsTableCellView.m | |
// Crew | |
// | |
// Created by John Wright on 8/18/11. | |
// Copyright 2011 QuickLeft. All rights reserved. | |
// | |
#import "PostCellView.h" | |
#import "Crew.h" | |
@interface PostCellView() | |
- (void) figureOut; | |
@end | |
@implementation PostCellView | |
@synthesize type; | |
@synthesize mainText; | |
- (id)initWithStyle:(TUITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier | |
{ | |
if((self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])) { | |
textRenderer = [[TUITextRenderer alloc] init]; | |
self.textRenderers = [NSArray arrayWithObjects:textRenderer, nil]; | |
} | |
return self; | |
} | |
- (void)dealloc | |
{ | |
[textRenderer release]; | |
[profilePic release]; | |
[super dealloc]; | |
} | |
- (NSDictionary *)post; | |
{ | |
return post; | |
} | |
- (void) figureOut { | |
type = [post valueForKey:@"type"]; | |
NSString *message = [post valueForKey:@"message"]; | |
if (!message) message = type; | |
CGFloat textHeight = [message ab_sizeWithFont:[Crew crewFont]].height; | |
if (textHeight > 55) { | |
NSLog(@"yes!"); | |
} | |
TUIAttributedString *s = [TUIAttributedString stringWithString:message]; | |
s.color = [TUIColor blackColor]; | |
s.font = [Crew crewFont]; | |
textRenderer.attributedString = s; | |
[self setNeedsDisplay]; | |
// Profile pic data | |
NSData *profilePicData = [post valueForKey:@"picture"]; | |
if (profilePicData) { | |
profilePic = [[TUIImage imageWithData:profilePicData] retain]; | |
} | |
} | |
- (void)setPost:(NSDictionary *)newPost | |
{ | |
[post release]; | |
post = [newPost retain]; | |
[self figureOut]; | |
} | |
-(CGFloat) height { | |
CGFloat calcuatedHeight = 55.0; | |
if (textRenderer && textRenderer.attributedString && textRenderer.attributedString.size.height > calcuatedHeight) calcuatedHeight = textRenderer.size.height; | |
if (profilePic) { | |
if (profilePic.size.height > calcuatedHeight) calcuatedHeight = profilePic.size.height; | |
} | |
return calcuatedHeight; | |
} | |
- (void)drawRect:(CGRect)rect | |
{ | |
CGRect b = self.bounds; | |
CGContextRef ctx = TUIGraphicsGetCurrentContext(); | |
if(self.selected) { | |
// selected background | |
CGContextSetRGBFillColor(ctx, .88, .89, .94, 1); | |
CGContextFillRect(ctx, b); | |
} else { | |
// white background | |
CGContextSetRGBFillColor(ctx, 1, 1, 1, 1); | |
CGContextFillRect(ctx, b); | |
// emboss | |
CGContextSetRGBFillColor(ctx, 1, 1, 1, 0.9); // light at the top | |
CGContextFillRect(ctx, CGRectMake(0, b.size.height-1, b.size.width, 1)); | |
CGContextSetRGBFillColor(ctx, 0, 0, 0, 0.08); // dark at the bottom | |
CGContextFillRect(ctx, CGRectMake(0, 0, b.size.width, 1)); | |
} | |
if (profilePic) { | |
CGRect imageRect = CGRectOffset(b, 5, -5); | |
imageRect.size.height = 50; | |
imageRect.size.width = 50; | |
[profilePic drawInRect:imageRect]; | |
} | |
// text | |
CGRect textRect = CGRectOffset(b, 65, -15); | |
textRenderer.frame = textRect; // set the frame so it knows where to draw itself | |
[textRenderer draw]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment