Skip to content

Commit

Permalink
Merge pull request #57 from markcleonard/version-fix
Browse files Browse the repository at this point in the history
Fixed OS version logic for badge styling
  • Loading branch information
Tim Davies committed May 27, 2014
2 parents c880a1d + a187b91 commit 0f90246
Showing 1 changed file with 74 additions and 69 deletions.
143 changes: 74 additions & 69 deletions TDBadgedCell (xcode project)/TDBadgedCell.m
Original file line number Diff line number Diff line change
Expand Up @@ -46,26 +46,28 @@ - (void) drawRect:(CGRect)rect
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:font }];
#else
CGSize numberSize = [self.badgeString sizeWithFont:font];
#endif

CGSize numberSize;
if ([[[UIDevice currentDevice] systemVersion] compare:@"7.0" options:NSNumericSearch] != NSOrderedAscending) {
numberSize = [self.badgeString sizeWithAttributes:@{ NSFontAttributeName:font }];
} else {
numberSize = [self.badgeString sizeWithFont:font];
}

CGFloat radius = (__radius)?__radius:8.5;

// Set the badge background colours

#if __IPHONE_OS_VERSION_MIN_REQUIRED >= 70000
__defaultColor = [UIColor colorWithRed:0 green:0.478 blue:1 alpha:1.0];
__defaultHighlightColor = [UIColor whiteColor];
#else
__defaultColor = [UIColor colorWithRed:0.530f green:0.600f blue:0.738f alpha:1.000f];
__defaultHighlightColor = [UIColor whiteColor];
#endif
// Set the badge background colours
if ([[[UIDevice currentDevice] systemVersion] compare:@"7.0" options:NSNumericSearch] != NSOrderedAscending) {
__defaultColor = [UIColor colorWithRed:0 green:0.478 blue:1 alpha:1.0];
__defaultHighlightColor = [UIColor whiteColor];
} else {
__defaultColor = [UIColor colorWithRed:0.530f green:0.600f blue:0.738f alpha:1.000f];
__defaultHighlightColor = [UIColor whiteColor];

}

UIColor *colour;
if((__parent.selectionStyle != UITableViewCellSelectionStyleNone) && (__parent.highlighted || __parent.selected))
UIColor *colour;
if((__parent.selectionStyle != UITableViewCellSelectionStyleNone) && (__parent.highlighted || __parent.selected))
if (__badgeColorHighlighted)
colour = __badgeColorHighlighted;
else
Expand All @@ -90,59 +92,60 @@ - (void) drawRect:(CGRect)rect
CGContextSaveGState(context);
[__badge renderInContext:context];
CGContextRestoreGState(context);


// Create a frame for the badge text
CGRect bounds = CGRectMake((rect.size.width / 2) - (numberSize.width / 2) ,
((rect.size.height / 2) - (numberSize.height / 2)),
numberSize.width + 12 , numberSize.height);

// Draw and clip the badge text from the badge shape
#if __IPHONE_OS_VERSION_MIN_REQUIRED >= 70000
UIColor *stringColor = nil;
if((__parent.highlighted || __parent.selected)) {

if ([[[UIDevice currentDevice] systemVersion] compare:@"7.0" options:NSNumericSearch] != NSOrderedAscending) {
UIColor *stringColor = nil;
if((__parent.highlighted || __parent.selected)) {
stringColor = __badgeTextColorHighlighted ? __badgeTextColorHighlighted : UIColor.lightGrayColor;
}
else {

if(__badgeTextColor) {
stringColor = __badgeTextColor;
} else {
stringColor = [UIColor clearColor];
CGContextSetBlendMode(context, kCGBlendModeClear);
}
}

NSMutableParagraphStyle *paragraph = [[NSMutableParagraphStyle alloc] init];
[paragraph setLineBreakMode:NSLineBreakByClipping];
[__badgeString drawInRect:bounds withAttributes:@{ NSFontAttributeName:font,
NSParagraphStyleAttributeName:paragraph,
NSForegroundColorAttributeName:stringColor}];
#if !__has_feature(objc_arc)
[paragraph release];
#endif

#else
if((__parent.highlighted || __parent.selected)) {
if (__badgeTextColorHighlighted) {
CGContextSetFillColorWithColor(context, __badgeTextColorHighlighted.CGColor);
}
else {
CGContextSetBlendMode(context, kCGBlendModeClear);

if(__badgeTextColor) {
stringColor = __badgeTextColor;
} else {
stringColor = [UIColor clearColor];
CGContextSetBlendMode(context, kCGBlendModeClear);
}
}
}
else {
if (__badgeTextColor) {
CGContextSetFillColorWithColor(context, __badgeTextColor.CGColor);

NSMutableParagraphStyle *paragraph = [[NSMutableParagraphStyle alloc] init];
[paragraph setLineBreakMode:NSLineBreakByClipping];
[__badgeString drawInRect:bounds withAttributes:@{ NSFontAttributeName:font,
NSParagraphStyleAttributeName:paragraph,
NSForegroundColorAttributeName:stringColor}];
#if !__has_feature(objc_arc)
[paragraph release];
#endif

} else {

if((__parent.highlighted || __parent.selected)) {
if (__badgeTextColorHighlighted) {
CGContextSetFillColorWithColor(context, __badgeTextColorHighlighted.CGColor);
}
else {
CGContextSetBlendMode(context, kCGBlendModeClear);
}
}
else {
CGContextSetBlendMode(context, kCGBlendModeClear);
if (__badgeTextColor) {
CGContextSetFillColorWithColor(context, __badgeTextColor.CGColor);
}
else {
CGContextSetBlendMode(context, kCGBlendModeClear);
}
}

[__badgeString drawInRect:bounds withFont:font lineBreakMode:TDLineBreakModeClip];
}

[__badgeString drawInRect:bounds withFont:font lineBreakMode:TDLineBreakModeClip];
#endif

// Create an image from the new badge (Fast and easy to cache)
UIImage *outputImage = UIGraphicsGetImageFromCurrentImageContext();
Expand Down Expand Up @@ -265,22 +268,24 @@ - (void) layoutSubviews
else
[self.badge setHidden:NO];

#if __IPHONE_OS_VERSION_MIN_REQUIRED >= 70000
if (self.accessoryType != UITableViewCellAccessoryNone) {

self.badgeRightOffset = 0.f;
} else {

self.badgeRightOffset = 12.f;
if ([[[UIDevice currentDevice] systemVersion] compare:@"7.0" options:NSNumericSearch] != NSOrderedAscending) {
if (self.accessoryType != UITableViewCellAccessoryNone) {

self.badgeRightOffset = 0.f;
} else {

self.badgeRightOffset = 12.f;
}
}
#endif
// 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:font }];
#else
CGSize badgeSize = [self.badgeString sizeWithFont:font];
#endif

CGSize badgeSize;
if ([[[UIDevice currentDevice] systemVersion] compare:@"7.0" options:NSNumericSearch] != NSOrderedAscending) {
badgeSize = [self.badgeString sizeWithAttributes:@{ NSFontAttributeName:font }];
} else {
badgeSize = [self.badgeString sizeWithFont:font];
}
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),
badgeSize.width + 13, badgeSize.height + (50/badgeSize.height));
Expand Down

0 comments on commit 0f90246

Please sign in to comment.