Skip to content

Commit

Permalink
Merge pull request #33 from shazino/master
Browse files Browse the repository at this point in the history
Add option to use bold or non-bold system font
  • Loading branch information
Tim Davies committed Oct 2, 2013
2 parents f5e1270 + 47f267f commit 876c7ca
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
1 change: 1 addition & 0 deletions TDBadgedCell (xcode project)/TDBadgedCell.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
@property (nonatomic, TD_STRONG) UIColor *badgeTextColor;
@property (nonatomic, TD_STRONG) UIColor *badgeColorHighlighted;
@property (nonatomic, assign) BOOL showShadow;
@property (nonatomic, assign) BOOL boldFont;
@property (nonatomic, assign) CGFloat fontSize;
@property (nonatomic, assign) CGFloat radius;

Expand Down
21 changes: 14 additions & 7 deletions TDBadgedCell (xcode project)/TDBadgedCell.m
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,19 @@

@implementation TDBadgeView

@synthesize width=__width, badgeString=__badgeString, parent=__parent, badgeColor=__badgeColor, badgeTextColor=__badgeTextColor, badgeColorHighlighted=__badgeColorHighlighted, showShadow=__showShadow, radius=__radius;
@synthesize width=__width, badgeString=__badgeString, parent=__parent, badgeColor=__badgeColor, badgeTextColor=__badgeTextColor, badgeColorHighlighted=__badgeColorHighlighted, showShadow=__showShadow, boldFont=__boldFont, radius=__radius;

- (id) initWithFrame:(CGRect)frame
{
if ((self = [super initWithFrame:frame]))
{
self.backgroundColor = [UIColor clearColor];
self.fontSize = 11.f;

if ([[[UIDevice currentDevice] systemVersion] compare:@"7.0" options:NSNumericSearch] != NSOrderedAscending)
self.boldFont = NO;
else
self.boldFont = YES;
}

return self;
Expand All @@ -40,10 +45,11 @@ - (void) drawRect:(CGRect)rect
// Set up variable for drawing
CGFloat scale = [[UIScreen mainScreen] scale];
CGFloat fontsize = self.fontSize;
UIFont *font = self.boldFont ? [UIFont boldSystemFontOfSize:fontsize] : [UIFont systemFontOfSize:fontsize];
#if __IPHONE_OS_VERSION_MIN_REQUIRED >= 70000
CGSize numberSize = [self.badgeString sizeWithAttributes:@{ NSFontAttributeName:[UIFont boldSystemFontOfSize:fontsize] }];
CGSize numberSize = [self.badgeString sizeWithAttributes:@{ NSFontAttributeName:font }];
#else
CGSize numberSize = [self.badgeString sizeWithFont:[UIFont boldSystemFontOfSize:fontsize]];
CGSize numberSize = [self.badgeString sizeWithFont:font];
#endif
CGFloat radius = (__radius)?__radius:4.0;

Expand Down Expand Up @@ -90,10 +96,10 @@ - (void) drawRect:(CGRect)rect
#if __IPHONE_OS_VERSION_MIN_REQUIRED >= 70000
NSMutableParagraphStyle *paragraph = [[NSMutableParagraphStyle alloc] init];
[paragraph setLineBreakMode:NSLineBreakByClipping];
[__badgeString drawInRect:bounds withAttributes:@{ NSFontAttributeName:[UIFont boldSystemFontOfSize:fontsize],
[__badgeString drawInRect:bounds withAttributes:@{ NSFontAttributeName:font,
NSParagraphStyleAttributeName:paragraph}];
#else
[__badgeString drawInRect:bounds withFont:[UIFont boldSystemFontOfSize:fontsize] lineBreakMode:TDLineBreakModeClip];
[__badgeString drawInRect:bounds withFont:font lineBreakMode:TDLineBreakModeClip];
#endif

// Create an image from the new badge (Fast and easy to cache)
Expand Down Expand Up @@ -215,10 +221,11 @@ - (void) layoutSubviews


// Calculate the size of the bage from the badge string
UIFont *font = self.badge.boldFont ? [UIFont boldSystemFontOfSize:self.badge.fontSize] : [UIFont systemFontOfSize:self.badge.fontSize];
#if __IPHONE_OS_VERSION_MIN_REQUIRED >= 70000
CGSize badgeSize = [self.badgeString sizeWithAttributes:@{ NSFontAttributeName:[UIFont boldSystemFontOfSize:self.badge.fontSize] }];
CGSize badgeSize = [self.badgeString sizeWithAttributes:@{ NSFontAttributeName:font }];
#else
CGSize badgeSize = [self.badgeString sizeWithFont:[UIFont boldSystemFontOfSize: self.badge.fontSize]];
CGSize badgeSize = [self.badgeString sizeWithFont:font];
#endif
CGRect badgeframe = CGRectMake(self.contentView.frame.size.width - (badgeSize.width + 13 + self.badgeRightOffset),
(CGFloat)round((self.contentView.frame.size.height - (badgeSize.height + (50/badgeSize.height))) / 2),
Expand Down

0 comments on commit 876c7ca

Please sign in to comment.