Skip to content

Instantly share code, notes, and snippets.

@mrjjwright
Created August 20, 2011 17:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrjjwright/1159407 to your computer and use it in GitHub Desktop.
Save mrjjwright/1159407 to your computer and use it in GitHub Desktop.
Calculate height for a custom cell
//
// 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