Skip to content

Commit

Permalink
added a 3rd info label positioned below the detailTextLabel
Browse files Browse the repository at this point in the history
  • Loading branch information
ettore committed Dec 14, 2012
1 parent 8a24c1e commit fc16b47
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 57 deletions.
27 changes: 17 additions & 10 deletions CLCGCell.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,31 +39,36 @@

/*!
@class CLCGCell
@discussion This cell loads an image on the left side, renders multiline text
in its textLabel, adds a 1-line gray detailTextLabel and a standard disclosure
indicator on the right side. It also renders itself with a different
background color if it's set as a emphasized cell. The layout of
all components is performed accordingly, adding padding between elements.
@abstract A reusable cell capable of rendering 3 blocks of text and an image.
@discussion This cell loads an image on the left side and renders multiline
text in the standard UITableViewCell's textLabel and detailTextLabel.
It also adds an additional `infoTextLabel` positioned below detailTextLabel,
also with multiline text. The detailTextLabel is rendered in gray.
A standard disclosure indicator is added by default on the right side.
This cell is also able to render itself with a different background color
if it's set as an "emphasized" cell.
The layout of all components is performed accordingly, adding padding
between elements.
*/
@interface CLCGCell : UITableViewCell
{
UIFont *mTextFont;
UIFont *mDetailFont;
UILabel *mInfoTextLabel;
NSString *mImgUrl;
CGFloat mImgW;
CGFloat mImgH;
CGFloat mPadding;
UIColor *mNormalColor;
UIColor *mEmphasizedColor;
BOOL mEmphasized;
id mContext;
}

@property(nonatomic,retain) UIFont *textFont;
@property(nonatomic,retain) UIFont *detailFont;
@property(nonatomic,retain) NSString *imgUrl;
@property(nonatomic,assign) BOOL emphasized;
@property(nonatomic,retain) id context;//should this be assign?
@property(nonatomic,readonly) CGFloat padding;
@property(nonatomic,retain) UILabel *infoTextLabel;
@property(nonatomic,retain) UIColor *normalColor;

/*! Designated initializer. */
-(id)initWithImageWidth:(CGFloat)w
Expand All @@ -82,8 +87,10 @@
*/
+(CGFloat)cellHeightForText:(NSString*)text
detailText:(NSString*)detailtext
infoText:(NSString*)infotext
font:(UIFont*)text_font
detailFont:(UIFont*)detail_font
infoFont:(UIFont*)info_font
maxWidth:(CGFloat)cell_maxw
imageW:(CGFloat)imgw
imageH:(CGFloat)imgh
Expand Down Expand Up @@ -116,7 +123,7 @@
accessory type.
*/
+(CGFloat)maxAccessoryWidth;

+(void)setMaxAccessoryWidth:(CGFloat)w;

@end

116 changes: 69 additions & 47 deletions CLCGCell.m
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
*/
#define CLCG_DEFAULT_ACCESSORY_TYPE_W 22.0f

static CGFloat sMaxAccessoryWidth = CLCG_DEFAULT_ACCESSORY_TYPE_W;


/*!
@discussion This accounts for left and right viewport padding added by iOS,
Expand All @@ -64,22 +66,24 @@ CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
#define CLCGCELL_IMG_DEFAULT_W 60.0f
#define CLCGCELL_IMG_DEFAULT_H 60.0f

@interface CLCGCell ()
@end

@implementation CLCGCell

@synthesize textFont = mTextFont;
@synthesize detailFont = mDetailFont;
@synthesize imgUrl = mImgUrl;
@synthesize context = mContext;
@synthesize emphasized = mEmphasized;
@synthesize padding = mPadding;

@synthesize infoTextLabel = mInfoTextLabel;
@synthesize normalColor = mNormalColor;

-(void)dealloc
{
CLCG_REL(mTextFont);
CLCG_REL(mDetailFont);
CLCG_REL(mInfoTextLabel);
CLCG_REL(mImgUrl);
CLCG_REL(mContext);
CLCG_REL(mNormalColor);
CLCG_REL(mEmphasizedColor);
[super dealloc];
}
Expand Down Expand Up @@ -118,10 +122,19 @@ -(id)initWithImageWidth:(CGFloat)w
[[self imageView] setFrame:CGRectMake(padding, padding, w, h)];
[[self imageView] setAutoresizingMask:UIViewAutoresizingNone];
[[self imageView] setContentMode:UIViewContentModeScaleAspectFit];
[self setTextFont:[UIFont boldSystemFontOfSize:15.0f]]; //reasonable default
[self setDetailFont:[UIFont systemFontOfSize:12.0f]]; //reasonable default
[[self textLabel] setFont:[UIFont boldSystemFontOfSize:15.0f]]; //reasonable default
[[self detailTextLabel] setFont:[UIFont systemFontOfSize:12.0f]];//reasonable default
[mInfoTextLabel setFont:[UIFont systemFontOfSize:12.0f]]; //reasonable default
mEmphasizedColor = [UIColor colorWithRed:1.0 green:0.98 blue:0.85 alpha:1.0];
[mEmphasizedColor retain];
[self setNormalColor:[UIColor whiteColor]];

// info label, this goes below the detail label
mInfoTextLabel = [[UILabel alloc] initWithFrame:CGRectZero];
[mInfoTextLabel setTextColor:[UIColor blackColor]];
[mInfoTextLabel setNumberOfLines:0];
[mInfoTextLabel setBackgroundColor:[UIColor clearColor]];
[self addSubview:mInfoTextLabel];

// needed for changing the background color
UIView *bgview = [[UIView alloc] initWithFrame:CGRectZero];
Expand All @@ -135,30 +148,12 @@ -(id)initWithImageWidth:(CGFloat)w
}


-(void)setTextFont:(UIFont*)f
{
[f retain];
[mTextFont release];
mTextFont = f;
[[self textLabel] setFont:f];
}


-(void)setDetailFont:(UIFont*)f
{
[f retain];
[mDetailFont release];
mDetailFont = f;
[[self detailTextLabel] setFont:f];
}


-(void)updateBackgroundColor
{
// Note: setting the background color on the contentView or even all the
// subviews doesn't take care of changing the background of the accessoryView.
// Setting the background of the accessoryView doesn't seem to work either.
UIColor *color = (mEmphasized ? mEmphasizedColor : [UIColor whiteColor]);
UIColor *color = (mEmphasized ? mEmphasizedColor : mNormalColor);
[[self backgroundView] setBackgroundColor:color];
}

Expand Down Expand Up @@ -202,34 +197,44 @@ -(void)showImage:(UIImage*)img
-(void)layoutSubviews
{
CGRect r;
CGFloat x, w;
CGSize sz;

[super layoutSubviews];

// layout image view
[[self imageView] setFrame:CGRectMake(mPadding, mPadding, mImgW, mImgH)];


// these should not change
const CGFloat cellh = [self h];
const CGFloat imgw = [[self imageView] w];
const CGFloat x = [self xRightOfImage];
const CGFloat w = [CLCGCell textLabelWidthWithCellW:[self w]
imageW:imgw
padding:mPadding];

// layout text label
w = [[self imageView] w];
x = [self xRightOfImage];
w = [CLCGCell textLabelWidthWithCellW:[self w] imageW:w padding:mPadding];
sz = CGSizeMake(w, [self h]);
sz = [[[self textLabel] text] sizeWithFont:mTextFont
sz = CGSizeMake(w, cellh);
sz = [[[self textLabel] text] sizeWithFont:[[self textLabel] font]
constrainedToSize:sz
lineBreakMode:UILineBreakModeWordWrap];

r = CGRectMake(x, mPadding, w, sz.height);
[[self textLabel] setFrame:r];

// layout detail label
sz = CGSizeMake(w, [self h]);
sz = [[[self detailTextLabel] text] sizeWithFont:mDetailFont
sz = CGSizeMake(w, cellh);
sz = [[[self detailTextLabel] text] sizeWithFont:[[self detailTextLabel] font]
constrainedToSize:sz
lineBreakMode:UILineBreakModeWordWrap];
w = sz.width;
r = CGRectMake(x, [[self textLabel] low] + (int)(mPadding/2), w, sz.height);
r = CGRectMake(x, [[self textLabel] low] + (int)(mPadding/2), sz.width, sz.height);
[[self detailTextLabel] setFrame:r];

// info text label
sz = CGSizeMake(w, cellh);
sz = [[mInfoTextLabel text] sizeWithFont:[mInfoTextLabel font]
constrainedToSize:sz
lineBreakMode:UILineBreakModeWordWrap];
r = CGRectMake(x, [[self detailTextLabel] low] + (int)(mPadding/2), w, sz.height);
[[self infoTextLabel] setFrame:r];
}


Expand All @@ -242,7 +247,13 @@ -(CGFloat)xRightOfImage

+(CGFloat)maxAccessoryWidth
{
return CLCG_DEFAULT_ACCESSORY_TYPE_W;
return sMaxAccessoryWidth;
}


+(void)setMaxAccessoryWidth:(CGFloat)w
{
sMaxAccessoryWidth = w;
}


Expand All @@ -255,36 +266,47 @@ +(CGFloat)textLabelWidthWithCellW:(CGFloat)maxw
}



+(CGFloat)cellHeightForText:(NSString*)text
detailText:(NSString*)detailtext
font:(UIFont*)text_font
detailFont:(UIFont*)detail_font
infoText:(NSString*)infotext
font:(UIFont*)text_font
detailFont:(UIFont*)detail_font
infoFont:(UIFont*)info_font
maxWidth:(CGFloat)cell_maxw
imageW:(CGFloat)imgw
imageH:(CGFloat)imgh
padding:(CGFloat)padding
{
CGSize sz;
CGFloat label_w, h;
const CGFloat cell_maxh = CLCG_MAX_CELL_H;

// adding mPadding for cell margins (L & R) and right margin of img
// Note: using `self` here seems necessary to properly invoke polymorphism on
// maxAccessoryWidth, called by textLabelWidthWithCellW:imageW:padding:.
label_w = [self textLabelWidthWithCellW:cell_maxw imageW:imgw padding:padding];

// measure main text size
sz = CGSizeMake(label_w, CLCG_MAX_CELL_H);
sz = CGSizeMake(label_w, cell_maxh);
sz = [text sizeWithFont:text_font
constrainedToSize:sz
lineBreakMode:UILineBreakModeWordWrap];
h = sz.height;

// add detail text size.
h += padding + [detailtext sizeWithFont:detail_font
constrainedToSize:CGSizeMake(label_w, CLCG_MAX_CELL_H)
if ([detailtext length] > 0) {
h += padding + [detailtext sizeWithFont:detail_font
constrainedToSize:CGSizeMake(label_w, cell_maxh)
lineBreakMode:UILineBreakModeWordWrap].height;
}

// now we have to add space for info text + 1 padding unit
if ([infotext length] > 0) {
h += padding + [infotext sizeWithFont:info_font
constrainedToSize:CGSizeMake(label_w, cell_maxh)
lineBreakMode:UILineBreakModeWordWrap].height;

}

// add padding above and below cell content
h = MAX(h, imgh) + padding*2;

Expand Down

0 comments on commit fc16b47

Please sign in to comment.