From 19b94ec6fa9b485bcb31b952fd3b77ce7f52a47b Mon Sep 17 00:00:00 2001 From: grant Date: Wed, 2 Dec 2015 14:23:25 +0800 Subject: [PATCH 1/8] add tinymode and backgroudImage --- Example/JSBadgeView.xcodeproj/project.pbxproj | 32 +++++ .../Base.lproj/Main_iPad.storyboard | 13 +- .../Base.lproj/Main_iPhone.storyboard | 13 +- .../AppIcon.appiconset/Contents.json | 17 ++- Example/JSBadgeView/JSViewController.m | 6 +- Example/Podfile.lock | 2 +- Pod/Classes/JSBadgeView.h | 36 +++++ Pod/Classes/JSBadgeView.m | 131 +++++++++++++----- 8 files changed, 197 insertions(+), 53 deletions(-) diff --git a/Example/JSBadgeView.xcodeproj/project.pbxproj b/Example/JSBadgeView.xcodeproj/project.pbxproj index 5a640da..95af697 100644 --- a/Example/JSBadgeView.xcodeproj/project.pbxproj +++ b/Example/JSBadgeView.xcodeproj/project.pbxproj @@ -206,6 +206,7 @@ 6003F587195388D20070C39A /* Frameworks */, 6003F588195388D20070C39A /* Resources */, 2B276D4DEFA48597D1EAFEA4 /* Copy Pods Resources */, + 9C99703B5313BC2699B98A48 /* Embed Pods Frameworks */, ); buildRules = ( ); @@ -225,6 +226,7 @@ 6003F5AB195388D20070C39A /* Frameworks */, 6003F5AC195388D20070C39A /* Resources */, 5AE0D8D1A81A1FC823A57EFA /* Copy Pods Resources */, + FFC0256144CDF6916E7885E6 /* Embed Pods Frameworks */, ); buildRules = ( ); @@ -353,6 +355,36 @@ shellScript = "diff \"${PODS_ROOT}/../Podfile.lock\" \"${PODS_ROOT}/Manifest.lock\" > /dev/null\nif [[ $? != 0 ]] ; then\n cat << EOM\nerror: The sandbox is not in sync with the Podfile.lock. Run 'pod install' or update your CocoaPods installation.\nEOM\n exit 1\nfi\n"; showEnvVarsInLog = 0; }; + 9C99703B5313BC2699B98A48 /* Embed Pods Frameworks */ = { + isa = PBXShellScriptBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + inputPaths = ( + ); + name = "Embed Pods Frameworks"; + outputPaths = ( + ); + runOnlyForDeploymentPostprocessing = 0; + shellPath = /bin/sh; + shellScript = "\"${SRCROOT}/Pods/Target Support Files/Pods-JSBadgeView/Pods-JSBadgeView-frameworks.sh\"\n"; + showEnvVarsInLog = 0; + }; + FFC0256144CDF6916E7885E6 /* Embed Pods Frameworks */ = { + isa = PBXShellScriptBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + inputPaths = ( + ); + name = "Embed Pods Frameworks"; + outputPaths = ( + ); + runOnlyForDeploymentPostprocessing = 0; + shellPath = /bin/sh; + shellScript = "\"${SRCROOT}/Pods/Target Support Files/Pods-Tests/Pods-Tests-frameworks.sh\"\n"; + showEnvVarsInLog = 0; + }; /* End PBXShellScriptBuildPhase section */ /* Begin PBXSourcesBuildPhase section */ diff --git a/Example/JSBadgeView/Base.lproj/Main_iPad.storyboard b/Example/JSBadgeView/Base.lproj/Main_iPad.storyboard index 5ff1a9c..f037c29 100644 --- a/Example/JSBadgeView/Base.lproj/Main_iPad.storyboard +++ b/Example/JSBadgeView/Base.lproj/Main_iPad.storyboard @@ -1,10 +1,11 @@ - + - + + - + @@ -15,6 +16,7 @@ + @@ -22,9 +24,4 @@ - - - - - diff --git a/Example/JSBadgeView/Base.lproj/Main_iPhone.storyboard b/Example/JSBadgeView/Base.lproj/Main_iPhone.storyboard index 5d93c46..61e6910 100644 --- a/Example/JSBadgeView/Base.lproj/Main_iPhone.storyboard +++ b/Example/JSBadgeView/Base.lproj/Main_iPhone.storyboard @@ -1,10 +1,11 @@ - + - + + - + @@ -15,6 +16,7 @@ + @@ -22,9 +24,4 @@ - - - - - diff --git a/Example/JSBadgeView/Images.xcassets/AppIcon.appiconset/Contents.json b/Example/JSBadgeView/Images.xcassets/AppIcon.appiconset/Contents.json index f697f61..36d2c80 100644 --- a/Example/JSBadgeView/Images.xcassets/AppIcon.appiconset/Contents.json +++ b/Example/JSBadgeView/Images.xcassets/AppIcon.appiconset/Contents.json @@ -5,16 +5,31 @@ "size" : "29x29", "scale" : "2x" }, + { + "idiom" : "iphone", + "size" : "29x29", + "scale" : "3x" + }, { "idiom" : "iphone", "size" : "40x40", "scale" : "2x" }, + { + "idiom" : "iphone", + "size" : "40x40", + "scale" : "3x" + }, { "idiom" : "iphone", "size" : "60x60", "scale" : "2x" }, + { + "idiom" : "iphone", + "size" : "60x60", + "scale" : "3x" + }, { "idiom" : "ipad", "size" : "29x29", @@ -50,4 +65,4 @@ "version" : 1, "author" : "xcode" } -} +} \ No newline at end of file diff --git a/Example/JSBadgeView/JSViewController.m b/Example/JSBadgeView/JSViewController.m index 24e2c76..42f70ab 100644 --- a/Example/JSBadgeView/JSViewController.m +++ b/Example/JSBadgeView/JSViewController.m @@ -20,7 +20,7 @@ #import -#define kNumBadges 100 +#define kNumBadges 120 #define kViewBackgroundColor [UIColor colorWithRed:0.357 green:0.757 blue:0.357 alpha:1] @@ -76,7 +76,9 @@ - (void)loadView JSBadgeView *badgeView = [[JSBadgeView alloc] initWithParentView:rectangle alignment:JSBadgeViewAlignmentTopRight]; badgeView.badgeText = [NSString stringWithFormat:@"%d", i]; - + if (i >= 100) { + badgeView.tinyMode = YES; + } [scrollView addSubview:rectangle]; [scrollView sendSubviewToBack:rectangle]; diff --git a/Example/Podfile.lock b/Example/Podfile.lock index 1131a77..24cc446 100644 --- a/Example/Podfile.lock +++ b/Example/Podfile.lock @@ -17,4 +17,4 @@ SPEC CHECKSUMS: JSBadgeView: 3861ddee41094284d9468e6d0af5107f703aaac5 Specta: 96fe05fe5c7348b5223f85e862904f6e832abb14 -COCOAPODS: 0.37.1 +COCOAPODS: 0.39.0 diff --git a/Pod/Classes/JSBadgeView.h b/Pod/Classes/JSBadgeView.h index 8128e9d..591aad2 100644 --- a/Pod/Classes/JSBadgeView.h +++ b/Pod/Classes/JSBadgeView.h @@ -92,6 +92,42 @@ typedef NS_ENUM(NSUInteger, JSBadgeViewAlignment) */ @property (nonatomic, assign) CGFloat badgeMinWidth UI_APPEARANCE_SELECTOR; +/** + * The margin add to the text left and right,both left and right use badgeViewTextSideMargin/2. + */ +@property (nonatomic,assign) CGFloat badgeViewTextSideMargin UI_APPEARANCE_SELECTOR; + +/** + * The corner radius use to the display rect. + */ +@property (nonatomic,assign) CGFloat badgeViewCornerRadius UI_APPEARANCE_SELECTOR; + +/** + * The height for the hold rect of text. + */ +@property (nonatomic,assign) CGFloat badgeViewHeight UI_APPEARANCE_SELECTOR; +/** + * shadow radius, if it > 0, we will draw a shadow. + */ +@property (nonatomic,assign) CGFloat badgeViewShadowRadius UI_APPEARANCE_SELECTOR; +/** + * will you prefer a backgroud image? it will auto scale to fit the badge size with the same radio. + */ +@property (nonatomic,strong) UIImage* badgeBackgroundImage UI_APPEARANCE_SELECTOR; +/** + * tiny mode,not show text,just a small cire. + */ +@property (nonatomic,assign) BOOL tinyMode UI_APPEARANCE_SELECTOR; +/** + * tiny mode's backgroud color. + */ +@property (nonatomic,strong) UIColor* tinyModeBackgroundColor UI_APPEARANCE_SELECTOR; + +/** + * tiny mode's radius. + */ +@property (nonatomic,assign) CGFloat badgeViewTinyRadius UI_APPEARANCE_SELECTOR; + /** * Optionally init using this method to have the badge automatically added to another view. */ diff --git a/Pod/Classes/JSBadgeView.m b/Pod/Classes/JSBadgeView.m index e3c5366..e1b4d4e 100644 --- a/Pod/Classes/JSBadgeView.m +++ b/Pod/Classes/JSBadgeView.m @@ -41,10 +41,6 @@ of this software and associated documentation files (the "Software"), to deal #define JSBadgeViewSilenceDeprecatedMethodEnd() #endif -static const CGFloat JSBadgeViewShadowRadius = 1.0f; -static const CGFloat JSBadgeViewHeight = 16.0f; -static const CGFloat JSBadgeViewTextSideMargin = 8.0f; -static const CGFloat JSBadgeViewCornerRadius = 10.0f; // Thanks to Peter Steinberger: https://gist.github.com/steipete/6526860 static BOOL JSBadgeViewIsUIKitFlatMode(void) @@ -78,6 +74,13 @@ + (void)applyCommonStyle badgeViewAppearanceProxy.badgeBackgroundColor = UIColor.redColor; badgeViewAppearanceProxy.badgeTextFont = [UIFont boldSystemFontOfSize:UIFont.systemFontSize]; badgeViewAppearanceProxy.badgeTextColor = UIColor.whiteColor; + badgeViewAppearanceProxy.badgeViewShadowRadius = 1.0f; + badgeViewAppearanceProxy.badgeViewHeight = 16.0f; + badgeViewAppearanceProxy.badgeViewTextSideMargin = 8.0f; + badgeViewAppearanceProxy.badgeViewCornerRadius = 10.0f; + badgeViewAppearanceProxy.tinyMode = NO; + badgeViewAppearanceProxy.tinyModeBackgroundColor = UIColor.redColor; + badgeViewAppearanceProxy.badgeViewTinyRadius = 5.0f; } + (void)applyLegacyStyle @@ -131,6 +134,21 @@ - (id)initWithParentView:(UIView *)parentView alignment:(JSBadgeViewAlignment)al return self; } +-(void) setTinyMode:(BOOL)tinyMode +{ + if ( tinyMode == self.tinyMode) { + return; + } + _tinyMode = tinyMode; + [self setNeedsLayout]; +} + +-(void)setBadgeBackgroundImage:(UIImage *)badgeBackgroundImage +{ + _badgeBackgroundImage = badgeBackgroundImage; + [self setNeedsLayout]; +} + #pragma mark - Layout - (CGFloat)marginToDrawInside @@ -141,19 +159,25 @@ - (CGFloat)marginToDrawInside - (void)layoutSubviews { [super layoutSubviews]; - + + CGRect newFrame = self.frame; - const CGRect superviewBounds = CGRectIsEmpty(_frameToPositionInRelationWith) ? self.superview.bounds : _frameToPositionInRelationWith; + const CGRect superviewBounds = CGRectIsEmpty(self.frameToPositionInRelationWith) ? self.superview.bounds : self.frameToPositionInRelationWith; const CGFloat textWidth = [self sizeOfTextForCurrentSettings].width; - + const CGFloat marginToDrawInside = [self marginToDrawInside]; - const CGFloat viewWidth = MAX(_badgeMinWidth, textWidth + JSBadgeViewTextSideMargin + (marginToDrawInside * 2)); - const CGFloat viewHeight = JSBadgeViewHeight + (marginToDrawInside * 2); + CGFloat viewWidth = MAX(self.badgeMinWidth, textWidth + self.badgeViewTextSideMargin + (marginToDrawInside * 2)); + CGFloat viewHeight = self.badgeViewHeight + (marginToDrawInside * 2); const CGFloat superviewWidth = superviewBounds.size.width; const CGFloat superviewHeight = superviewBounds.size.height; + if (self.tinyMode) { + viewWidth = 2*(self.badgeViewTinyRadius+self.badgeViewShadowRadius); + viewHeight = viewWidth; + } + newFrame.size.width = MAX(viewWidth, viewHeight); newFrame.size.height = viewHeight; @@ -198,8 +222,8 @@ - (void)layoutSubviews NSAssert(NO, @"Unimplemented JSBadgeAligment type %lul", (unsigned long)self.badgeAlignment); } - newFrame.origin.x += _badgePositionAdjustment.x; - newFrame.origin.y += _badgePositionAdjustment.y; + newFrame.origin.x += self.badgePositionAdjustment.x; + newFrame.origin.y += self.badgePositionAdjustment.y; // Do not set frame directly so we do not interfere with any potential transform set on the view. self.bounds = CGRectIntegral(CGRectMake(0, 0, CGRectGetWidth(newFrame), CGRectGetHeight(newFrame))); @@ -339,32 +363,65 @@ - (void)setBadgeShadowSize:(CGSize)badgeShadowSize - (void)drawRect:(CGRect)rect { + const BOOL anyTextToDraw = (self.badgeText.length > 0); if (anyTextToDraw) { CGContextRef ctx = UIGraphicsGetCurrentContext(); - + const CGFloat marginToDrawInside = [self marginToDrawInside]; const CGRect rectToDraw = CGRectInset(rect, marginToDrawInside, marginToDrawInside); - UIBezierPath *borderPath = [UIBezierPath bezierPathWithRoundedRect:rectToDraw byRoundingCorners:(UIRectCorner)UIRectCornerAllCorners cornerRadii:CGSizeMake(JSBadgeViewCornerRadius, JSBadgeViewCornerRadius)]; + UIBezierPath *borderPath; + if (self.tinyMode) { + borderPath= [UIBezierPath bezierPathWithRoundedRect:rectToDraw byRoundingCorners:(UIRectCorner)UIRectCornerAllCorners cornerRadii:CGSizeMake(self.badgeViewTinyRadius+marginToDrawInside, self.badgeViewTinyRadius+marginToDrawInside)]; + } + else{ + borderPath= [UIBezierPath bezierPathWithRoundedRect:rectToDraw byRoundingCorners:(UIRectCorner)UIRectCornerAllCorners cornerRadii:CGSizeMake(self.badgeViewCornerRadius, self.badgeViewCornerRadius)]; + } + CGColorRef bgCGColor = self.tinyMode ? self.tinyModeBackgroundColor.CGColor : self.badgeBackgroundColor.CGColor; - /* Background and shadow */ - CGContextSaveGState(ctx); - { - CGContextAddPath(ctx, borderPath.CGPath); + if (nil == self.badgeBackgroundImage) { + CGContextSetFillColorWithColor(ctx, bgCGColor); + /* Background and shadow */ + CGContextSaveGState(ctx); + { + CGContextAddPath(ctx, borderPath.CGPath); + if (!self.tinyMode && self.badgeViewShadowRadius > 0 ) { + CGContextSetShadowWithColor(ctx, self.badgeShadowSize, self.badgeViewShadowRadius, self.badgeShadowColor.CGColor); + } + + CGContextDrawPath(ctx, kCGPathFill); + } + CGContextRestoreGState(ctx); + + } + else{ + + CGFloat width = self.badgeBackgroundImage.size.width; + CGFloat height = self.badgeBackgroundImage.size.height; - CGContextSetFillColorWithColor(ctx, self.badgeBackgroundColor.CGColor); - CGContextSetShadowWithColor(ctx, self.badgeShadowSize, JSBadgeViewShadowRadius, self.badgeShadowColor.CGColor); + float verticalRadio = rectToDraw.size.height*1.0/height; + float horizontalRadio = rectToDraw.size.width*1.0/width; + + float radio = 1; + radio = verticalRadio < horizontalRadio ? horizontalRadio : verticalRadio; + + CGRect displayRect = CGRectMake((rectToDraw.size.width - width*radio)/2.0f, + (rectToDraw.size.height-height*radio)/2.0f, + width*radio, + height*radio); + + [self.badgeBackgroundImage drawInRect:displayRect]; - CGContextDrawPath(ctx, kCGPathFill); } - CGContextRestoreGState(ctx); + + const BOOL colorForOverlayPresent = self.badgeOverlayColor && ![self.badgeOverlayColor isEqual:[UIColor clearColor]]; - if (colorForOverlayPresent) + if ( colorForOverlayPresent) { /* Gradient overlay */ CGContextSaveGState(ctx); @@ -379,10 +436,10 @@ - (void)drawRect:(CGRect)rect rectToDraw.origin.y - ceilf(height * 0.5), width, height); - + CGContextAddEllipseInRect(ctx, rectForOverlayCircle); CGContextSetFillColorWithColor(ctx, self.badgeOverlayColor.CGColor); - + CGContextDrawPath(ctx, kCGPathFill); } CGContextRestoreGState(ctx); @@ -400,28 +457,36 @@ - (void)drawRect:(CGRect)rect } CGContextRestoreGState(ctx); + if (self.tinyMode) { + return; + } /* Text */ - + CGContextSaveGState(ctx); { CGContextSetFillColorWithColor(ctx, self.badgeTextColor.CGColor); - CGContextSetShadowWithColor(ctx, self.badgeTextShadowOffset, 1.0, self.badgeTextShadowColor.CGColor); + if (self.badgeViewShadowRadius > 0) { + CGContextSetShadowWithColor(ctx, self.badgeTextShadowOffset, 1.0, self.badgeTextShadowColor.CGColor); + } CGRect textFrame = rectToDraw; const CGSize textSize = [self sizeOfTextForCurrentSettings]; textFrame.size.height = textSize.height; - textFrame.origin.y = rectToDraw.origin.y + floorf((rectToDraw.size.height - textFrame.size.height) / 2.0f); - - JSBadgeViewSilenceDeprecatedMethodStart(); + textFrame.origin.y = rectToDraw.origin.y + ceilf((rectToDraw.size.height - textFrame.size.height) / 2.0f); + + NSMutableParagraphStyle *textStyle = [[NSMutableParagraphStyle defaultParagraphStyle] mutableCopy]; + textStyle.lineBreakMode = NSLineBreakByClipping; + textStyle.alignment = NSTextAlignmentCenter; + [self.badgeText drawInRect:textFrame - withFont:self.badgeTextFont - lineBreakMode:NSLineBreakByClipping - alignment:NSTextAlignmentCenter]; - JSBadgeViewSilenceDeprecatedMethodEnd(); + withAttributes:@{NSFontAttributeName:self.badgeTextFont, + NSParagraphStyleAttributeName: textStyle}]; + } CGContextRestoreGState(ctx); } } + @end \ No newline at end of file From f52661cfc5f17713aa5a544216039f6ec8ce7e04 Mon Sep 17 00:00:00 2001 From: grant Date: Wed, 2 Dec 2015 14:28:20 +0800 Subject: [PATCH 2/8] Revert "add tinymode and backgroudImage" This reverts commit 19b94ec6fa9b485bcb31b952fd3b77ce7f52a47b. --- Example/JSBadgeView.xcodeproj/project.pbxproj | 32 ----- .../Base.lproj/Main_iPad.storyboard | 13 +- .../Base.lproj/Main_iPhone.storyboard | 13 +- .../AppIcon.appiconset/Contents.json | 17 +-- Example/JSBadgeView/JSViewController.m | 6 +- Example/Podfile.lock | 2 +- Pod/Classes/JSBadgeView.h | 36 ----- Pod/Classes/JSBadgeView.m | 131 +++++------------- 8 files changed, 53 insertions(+), 197 deletions(-) diff --git a/Example/JSBadgeView.xcodeproj/project.pbxproj b/Example/JSBadgeView.xcodeproj/project.pbxproj index 95af697..5a640da 100644 --- a/Example/JSBadgeView.xcodeproj/project.pbxproj +++ b/Example/JSBadgeView.xcodeproj/project.pbxproj @@ -206,7 +206,6 @@ 6003F587195388D20070C39A /* Frameworks */, 6003F588195388D20070C39A /* Resources */, 2B276D4DEFA48597D1EAFEA4 /* Copy Pods Resources */, - 9C99703B5313BC2699B98A48 /* Embed Pods Frameworks */, ); buildRules = ( ); @@ -226,7 +225,6 @@ 6003F5AB195388D20070C39A /* Frameworks */, 6003F5AC195388D20070C39A /* Resources */, 5AE0D8D1A81A1FC823A57EFA /* Copy Pods Resources */, - FFC0256144CDF6916E7885E6 /* Embed Pods Frameworks */, ); buildRules = ( ); @@ -355,36 +353,6 @@ shellScript = "diff \"${PODS_ROOT}/../Podfile.lock\" \"${PODS_ROOT}/Manifest.lock\" > /dev/null\nif [[ $? != 0 ]] ; then\n cat << EOM\nerror: The sandbox is not in sync with the Podfile.lock. Run 'pod install' or update your CocoaPods installation.\nEOM\n exit 1\nfi\n"; showEnvVarsInLog = 0; }; - 9C99703B5313BC2699B98A48 /* Embed Pods Frameworks */ = { - isa = PBXShellScriptBuildPhase; - buildActionMask = 2147483647; - files = ( - ); - inputPaths = ( - ); - name = "Embed Pods Frameworks"; - outputPaths = ( - ); - runOnlyForDeploymentPostprocessing = 0; - shellPath = /bin/sh; - shellScript = "\"${SRCROOT}/Pods/Target Support Files/Pods-JSBadgeView/Pods-JSBadgeView-frameworks.sh\"\n"; - showEnvVarsInLog = 0; - }; - FFC0256144CDF6916E7885E6 /* Embed Pods Frameworks */ = { - isa = PBXShellScriptBuildPhase; - buildActionMask = 2147483647; - files = ( - ); - inputPaths = ( - ); - name = "Embed Pods Frameworks"; - outputPaths = ( - ); - runOnlyForDeploymentPostprocessing = 0; - shellPath = /bin/sh; - shellScript = "\"${SRCROOT}/Pods/Target Support Files/Pods-Tests/Pods-Tests-frameworks.sh\"\n"; - showEnvVarsInLog = 0; - }; /* End PBXShellScriptBuildPhase section */ /* Begin PBXSourcesBuildPhase section */ diff --git a/Example/JSBadgeView/Base.lproj/Main_iPad.storyboard b/Example/JSBadgeView/Base.lproj/Main_iPad.storyboard index f037c29..5ff1a9c 100644 --- a/Example/JSBadgeView/Base.lproj/Main_iPad.storyboard +++ b/Example/JSBadgeView/Base.lproj/Main_iPad.storyboard @@ -1,11 +1,10 @@ - + - - + - + @@ -16,7 +15,6 @@ - @@ -24,4 +22,9 @@ + + + + + diff --git a/Example/JSBadgeView/Base.lproj/Main_iPhone.storyboard b/Example/JSBadgeView/Base.lproj/Main_iPhone.storyboard index 61e6910..5d93c46 100644 --- a/Example/JSBadgeView/Base.lproj/Main_iPhone.storyboard +++ b/Example/JSBadgeView/Base.lproj/Main_iPhone.storyboard @@ -1,11 +1,10 @@ - + - - + - + @@ -16,7 +15,6 @@ - @@ -24,4 +22,9 @@ + + + + + diff --git a/Example/JSBadgeView/Images.xcassets/AppIcon.appiconset/Contents.json b/Example/JSBadgeView/Images.xcassets/AppIcon.appiconset/Contents.json index 36d2c80..f697f61 100644 --- a/Example/JSBadgeView/Images.xcassets/AppIcon.appiconset/Contents.json +++ b/Example/JSBadgeView/Images.xcassets/AppIcon.appiconset/Contents.json @@ -5,31 +5,16 @@ "size" : "29x29", "scale" : "2x" }, - { - "idiom" : "iphone", - "size" : "29x29", - "scale" : "3x" - }, { "idiom" : "iphone", "size" : "40x40", "scale" : "2x" }, - { - "idiom" : "iphone", - "size" : "40x40", - "scale" : "3x" - }, { "idiom" : "iphone", "size" : "60x60", "scale" : "2x" }, - { - "idiom" : "iphone", - "size" : "60x60", - "scale" : "3x" - }, { "idiom" : "ipad", "size" : "29x29", @@ -65,4 +50,4 @@ "version" : 1, "author" : "xcode" } -} \ No newline at end of file +} diff --git a/Example/JSBadgeView/JSViewController.m b/Example/JSBadgeView/JSViewController.m index 42f70ab..24e2c76 100644 --- a/Example/JSBadgeView/JSViewController.m +++ b/Example/JSBadgeView/JSViewController.m @@ -20,7 +20,7 @@ #import -#define kNumBadges 120 +#define kNumBadges 100 #define kViewBackgroundColor [UIColor colorWithRed:0.357 green:0.757 blue:0.357 alpha:1] @@ -76,9 +76,7 @@ - (void)loadView JSBadgeView *badgeView = [[JSBadgeView alloc] initWithParentView:rectangle alignment:JSBadgeViewAlignmentTopRight]; badgeView.badgeText = [NSString stringWithFormat:@"%d", i]; - if (i >= 100) { - badgeView.tinyMode = YES; - } + [scrollView addSubview:rectangle]; [scrollView sendSubviewToBack:rectangle]; diff --git a/Example/Podfile.lock b/Example/Podfile.lock index 24cc446..1131a77 100644 --- a/Example/Podfile.lock +++ b/Example/Podfile.lock @@ -17,4 +17,4 @@ SPEC CHECKSUMS: JSBadgeView: 3861ddee41094284d9468e6d0af5107f703aaac5 Specta: 96fe05fe5c7348b5223f85e862904f6e832abb14 -COCOAPODS: 0.39.0 +COCOAPODS: 0.37.1 diff --git a/Pod/Classes/JSBadgeView.h b/Pod/Classes/JSBadgeView.h index 591aad2..8128e9d 100644 --- a/Pod/Classes/JSBadgeView.h +++ b/Pod/Classes/JSBadgeView.h @@ -92,42 +92,6 @@ typedef NS_ENUM(NSUInteger, JSBadgeViewAlignment) */ @property (nonatomic, assign) CGFloat badgeMinWidth UI_APPEARANCE_SELECTOR; -/** - * The margin add to the text left and right,both left and right use badgeViewTextSideMargin/2. - */ -@property (nonatomic,assign) CGFloat badgeViewTextSideMargin UI_APPEARANCE_SELECTOR; - -/** - * The corner radius use to the display rect. - */ -@property (nonatomic,assign) CGFloat badgeViewCornerRadius UI_APPEARANCE_SELECTOR; - -/** - * The height for the hold rect of text. - */ -@property (nonatomic,assign) CGFloat badgeViewHeight UI_APPEARANCE_SELECTOR; -/** - * shadow radius, if it > 0, we will draw a shadow. - */ -@property (nonatomic,assign) CGFloat badgeViewShadowRadius UI_APPEARANCE_SELECTOR; -/** - * will you prefer a backgroud image? it will auto scale to fit the badge size with the same radio. - */ -@property (nonatomic,strong) UIImage* badgeBackgroundImage UI_APPEARANCE_SELECTOR; -/** - * tiny mode,not show text,just a small cire. - */ -@property (nonatomic,assign) BOOL tinyMode UI_APPEARANCE_SELECTOR; -/** - * tiny mode's backgroud color. - */ -@property (nonatomic,strong) UIColor* tinyModeBackgroundColor UI_APPEARANCE_SELECTOR; - -/** - * tiny mode's radius. - */ -@property (nonatomic,assign) CGFloat badgeViewTinyRadius UI_APPEARANCE_SELECTOR; - /** * Optionally init using this method to have the badge automatically added to another view. */ diff --git a/Pod/Classes/JSBadgeView.m b/Pod/Classes/JSBadgeView.m index e1b4d4e..e3c5366 100644 --- a/Pod/Classes/JSBadgeView.m +++ b/Pod/Classes/JSBadgeView.m @@ -41,6 +41,10 @@ of this software and associated documentation files (the "Software"), to deal #define JSBadgeViewSilenceDeprecatedMethodEnd() #endif +static const CGFloat JSBadgeViewShadowRadius = 1.0f; +static const CGFloat JSBadgeViewHeight = 16.0f; +static const CGFloat JSBadgeViewTextSideMargin = 8.0f; +static const CGFloat JSBadgeViewCornerRadius = 10.0f; // Thanks to Peter Steinberger: https://gist.github.com/steipete/6526860 static BOOL JSBadgeViewIsUIKitFlatMode(void) @@ -74,13 +78,6 @@ + (void)applyCommonStyle badgeViewAppearanceProxy.badgeBackgroundColor = UIColor.redColor; badgeViewAppearanceProxy.badgeTextFont = [UIFont boldSystemFontOfSize:UIFont.systemFontSize]; badgeViewAppearanceProxy.badgeTextColor = UIColor.whiteColor; - badgeViewAppearanceProxy.badgeViewShadowRadius = 1.0f; - badgeViewAppearanceProxy.badgeViewHeight = 16.0f; - badgeViewAppearanceProxy.badgeViewTextSideMargin = 8.0f; - badgeViewAppearanceProxy.badgeViewCornerRadius = 10.0f; - badgeViewAppearanceProxy.tinyMode = NO; - badgeViewAppearanceProxy.tinyModeBackgroundColor = UIColor.redColor; - badgeViewAppearanceProxy.badgeViewTinyRadius = 5.0f; } + (void)applyLegacyStyle @@ -134,21 +131,6 @@ - (id)initWithParentView:(UIView *)parentView alignment:(JSBadgeViewAlignment)al return self; } --(void) setTinyMode:(BOOL)tinyMode -{ - if ( tinyMode == self.tinyMode) { - return; - } - _tinyMode = tinyMode; - [self setNeedsLayout]; -} - --(void)setBadgeBackgroundImage:(UIImage *)badgeBackgroundImage -{ - _badgeBackgroundImage = badgeBackgroundImage; - [self setNeedsLayout]; -} - #pragma mark - Layout - (CGFloat)marginToDrawInside @@ -159,25 +141,19 @@ - (CGFloat)marginToDrawInside - (void)layoutSubviews { [super layoutSubviews]; - - + CGRect newFrame = self.frame; - const CGRect superviewBounds = CGRectIsEmpty(self.frameToPositionInRelationWith) ? self.superview.bounds : self.frameToPositionInRelationWith; + const CGRect superviewBounds = CGRectIsEmpty(_frameToPositionInRelationWith) ? self.superview.bounds : _frameToPositionInRelationWith; const CGFloat textWidth = [self sizeOfTextForCurrentSettings].width; - + const CGFloat marginToDrawInside = [self marginToDrawInside]; - CGFloat viewWidth = MAX(self.badgeMinWidth, textWidth + self.badgeViewTextSideMargin + (marginToDrawInside * 2)); - CGFloat viewHeight = self.badgeViewHeight + (marginToDrawInside * 2); + const CGFloat viewWidth = MAX(_badgeMinWidth, textWidth + JSBadgeViewTextSideMargin + (marginToDrawInside * 2)); + const CGFloat viewHeight = JSBadgeViewHeight + (marginToDrawInside * 2); const CGFloat superviewWidth = superviewBounds.size.width; const CGFloat superviewHeight = superviewBounds.size.height; - if (self.tinyMode) { - viewWidth = 2*(self.badgeViewTinyRadius+self.badgeViewShadowRadius); - viewHeight = viewWidth; - } - newFrame.size.width = MAX(viewWidth, viewHeight); newFrame.size.height = viewHeight; @@ -222,8 +198,8 @@ - (void)layoutSubviews NSAssert(NO, @"Unimplemented JSBadgeAligment type %lul", (unsigned long)self.badgeAlignment); } - newFrame.origin.x += self.badgePositionAdjustment.x; - newFrame.origin.y += self.badgePositionAdjustment.y; + newFrame.origin.x += _badgePositionAdjustment.x; + newFrame.origin.y += _badgePositionAdjustment.y; // Do not set frame directly so we do not interfere with any potential transform set on the view. self.bounds = CGRectIntegral(CGRectMake(0, 0, CGRectGetWidth(newFrame), CGRectGetHeight(newFrame))); @@ -363,65 +339,32 @@ - (void)setBadgeShadowSize:(CGSize)badgeShadowSize - (void)drawRect:(CGRect)rect { - const BOOL anyTextToDraw = (self.badgeText.length > 0); if (anyTextToDraw) { CGContextRef ctx = UIGraphicsGetCurrentContext(); - + const CGFloat marginToDrawInside = [self marginToDrawInside]; const CGRect rectToDraw = CGRectInset(rect, marginToDrawInside, marginToDrawInside); - UIBezierPath *borderPath; - if (self.tinyMode) { - borderPath= [UIBezierPath bezierPathWithRoundedRect:rectToDraw byRoundingCorners:(UIRectCorner)UIRectCornerAllCorners cornerRadii:CGSizeMake(self.badgeViewTinyRadius+marginToDrawInside, self.badgeViewTinyRadius+marginToDrawInside)]; - } - else{ - borderPath= [UIBezierPath bezierPathWithRoundedRect:rectToDraw byRoundingCorners:(UIRectCorner)UIRectCornerAllCorners cornerRadii:CGSizeMake(self.badgeViewCornerRadius, self.badgeViewCornerRadius)]; - } - CGColorRef bgCGColor = self.tinyMode ? self.tinyModeBackgroundColor.CGColor : self.badgeBackgroundColor.CGColor; + UIBezierPath *borderPath = [UIBezierPath bezierPathWithRoundedRect:rectToDraw byRoundingCorners:(UIRectCorner)UIRectCornerAllCorners cornerRadii:CGSizeMake(JSBadgeViewCornerRadius, JSBadgeViewCornerRadius)]; - if (nil == self.badgeBackgroundImage) { - CGContextSetFillColorWithColor(ctx, bgCGColor); - /* Background and shadow */ - CGContextSaveGState(ctx); - { - CGContextAddPath(ctx, borderPath.CGPath); - if (!self.tinyMode && self.badgeViewShadowRadius > 0 ) { - CGContextSetShadowWithColor(ctx, self.badgeShadowSize, self.badgeViewShadowRadius, self.badgeShadowColor.CGColor); - } - - CGContextDrawPath(ctx, kCGPathFill); - } - CGContextRestoreGState(ctx); - - } - else{ - - CGFloat width = self.badgeBackgroundImage.size.width; - CGFloat height = self.badgeBackgroundImage.size.height; - - float verticalRadio = rectToDraw.size.height*1.0/height; - float horizontalRadio = rectToDraw.size.width*1.0/width; - - float radio = 1; - radio = verticalRadio < horizontalRadio ? horizontalRadio : verticalRadio; - - CGRect displayRect = CGRectMake((rectToDraw.size.width - width*radio)/2.0f, - (rectToDraw.size.height-height*radio)/2.0f, - width*radio, - height*radio); + /* Background and shadow */ + CGContextSaveGState(ctx); + { + CGContextAddPath(ctx, borderPath.CGPath); - [self.badgeBackgroundImage drawInRect:displayRect]; + CGContextSetFillColorWithColor(ctx, self.badgeBackgroundColor.CGColor); + CGContextSetShadowWithColor(ctx, self.badgeShadowSize, JSBadgeViewShadowRadius, self.badgeShadowColor.CGColor); + CGContextDrawPath(ctx, kCGPathFill); } - - + CGContextRestoreGState(ctx); const BOOL colorForOverlayPresent = self.badgeOverlayColor && ![self.badgeOverlayColor isEqual:[UIColor clearColor]]; - if ( colorForOverlayPresent) + if (colorForOverlayPresent) { /* Gradient overlay */ CGContextSaveGState(ctx); @@ -436,10 +379,10 @@ - (void)drawRect:(CGRect)rect rectToDraw.origin.y - ceilf(height * 0.5), width, height); - + CGContextAddEllipseInRect(ctx, rectForOverlayCircle); CGContextSetFillColorWithColor(ctx, self.badgeOverlayColor.CGColor); - + CGContextDrawPath(ctx, kCGPathFill); } CGContextRestoreGState(ctx); @@ -457,36 +400,28 @@ - (void)drawRect:(CGRect)rect } CGContextRestoreGState(ctx); - if (self.tinyMode) { - return; - } /* Text */ - + CGContextSaveGState(ctx); { CGContextSetFillColorWithColor(ctx, self.badgeTextColor.CGColor); - if (self.badgeViewShadowRadius > 0) { - CGContextSetShadowWithColor(ctx, self.badgeTextShadowOffset, 1.0, self.badgeTextShadowColor.CGColor); - } + CGContextSetShadowWithColor(ctx, self.badgeTextShadowOffset, 1.0, self.badgeTextShadowColor.CGColor); CGRect textFrame = rectToDraw; const CGSize textSize = [self sizeOfTextForCurrentSettings]; textFrame.size.height = textSize.height; - textFrame.origin.y = rectToDraw.origin.y + ceilf((rectToDraw.size.height - textFrame.size.height) / 2.0f); - - NSMutableParagraphStyle *textStyle = [[NSMutableParagraphStyle defaultParagraphStyle] mutableCopy]; - textStyle.lineBreakMode = NSLineBreakByClipping; - textStyle.alignment = NSTextAlignmentCenter; - + textFrame.origin.y = rectToDraw.origin.y + floorf((rectToDraw.size.height - textFrame.size.height) / 2.0f); + + JSBadgeViewSilenceDeprecatedMethodStart(); [self.badgeText drawInRect:textFrame - withAttributes:@{NSFontAttributeName:self.badgeTextFont, - NSParagraphStyleAttributeName: textStyle}]; - + withFont:self.badgeTextFont + lineBreakMode:NSLineBreakByClipping + alignment:NSTextAlignmentCenter]; + JSBadgeViewSilenceDeprecatedMethodEnd(); } CGContextRestoreGState(ctx); } } - @end \ No newline at end of file From 3777b15e8056c2da1e18a1b4636410f7d38d2971 Mon Sep 17 00:00:00 2001 From: grant Date: Wed, 2 Dec 2015 14:32:16 +0800 Subject: [PATCH 3/8] revert the storyboard, minify the modify --- .../JSBadgeView/Base.lproj/Main_iPad.storyboard | 13 +++++-------- .../Base.lproj/Main_iPhone.storyboard | 13 +++++-------- .../AppIcon.appiconset/Contents.json | 17 ++++++++++++++++- 3 files changed, 26 insertions(+), 17 deletions(-) diff --git a/Example/JSBadgeView/Base.lproj/Main_iPad.storyboard b/Example/JSBadgeView/Base.lproj/Main_iPad.storyboard index 5ff1a9c..f037c29 100644 --- a/Example/JSBadgeView/Base.lproj/Main_iPad.storyboard +++ b/Example/JSBadgeView/Base.lproj/Main_iPad.storyboard @@ -1,10 +1,11 @@ - + - + + - + @@ -15,6 +16,7 @@ + @@ -22,9 +24,4 @@ - - - - - diff --git a/Example/JSBadgeView/Base.lproj/Main_iPhone.storyboard b/Example/JSBadgeView/Base.lproj/Main_iPhone.storyboard index 5d93c46..61e6910 100644 --- a/Example/JSBadgeView/Base.lproj/Main_iPhone.storyboard +++ b/Example/JSBadgeView/Base.lproj/Main_iPhone.storyboard @@ -1,10 +1,11 @@ - + - + + - + @@ -15,6 +16,7 @@ + @@ -22,9 +24,4 @@ - - - - - diff --git a/Example/JSBadgeView/Images.xcassets/AppIcon.appiconset/Contents.json b/Example/JSBadgeView/Images.xcassets/AppIcon.appiconset/Contents.json index f697f61..36d2c80 100644 --- a/Example/JSBadgeView/Images.xcassets/AppIcon.appiconset/Contents.json +++ b/Example/JSBadgeView/Images.xcassets/AppIcon.appiconset/Contents.json @@ -5,16 +5,31 @@ "size" : "29x29", "scale" : "2x" }, + { + "idiom" : "iphone", + "size" : "29x29", + "scale" : "3x" + }, { "idiom" : "iphone", "size" : "40x40", "scale" : "2x" }, + { + "idiom" : "iphone", + "size" : "40x40", + "scale" : "3x" + }, { "idiom" : "iphone", "size" : "60x60", "scale" : "2x" }, + { + "idiom" : "iphone", + "size" : "60x60", + "scale" : "3x" + }, { "idiom" : "ipad", "size" : "29x29", @@ -50,4 +65,4 @@ "version" : 1, "author" : "xcode" } -} +} \ No newline at end of file From 96188d5a222ddcbbfc161ee3b2eafd3feb73d10c Mon Sep 17 00:00:00 2001 From: grant Date: Wed, 2 Dec 2015 16:24:59 +0800 Subject: [PATCH 4/8] =?UTF-8?q?=E5=9B=9E=E9=80=80storyboard?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Example/JSBadgeView/Base.lproj/Main_iPad.storyboard | 13 ++++++++----- .../JSBadgeView/Base.lproj/Main_iPhone.storyboard | 13 ++++++++----- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/Example/JSBadgeView/Base.lproj/Main_iPad.storyboard b/Example/JSBadgeView/Base.lproj/Main_iPad.storyboard index f037c29..5ff1a9c 100644 --- a/Example/JSBadgeView/Base.lproj/Main_iPad.storyboard +++ b/Example/JSBadgeView/Base.lproj/Main_iPad.storyboard @@ -1,11 +1,10 @@ - + - - + - + @@ -16,7 +15,6 @@ - @@ -24,4 +22,9 @@ + + + + + diff --git a/Example/JSBadgeView/Base.lproj/Main_iPhone.storyboard b/Example/JSBadgeView/Base.lproj/Main_iPhone.storyboard index 61e6910..5d93c46 100644 --- a/Example/JSBadgeView/Base.lproj/Main_iPhone.storyboard +++ b/Example/JSBadgeView/Base.lproj/Main_iPhone.storyboard @@ -1,11 +1,10 @@ - + - - + - + @@ -16,7 +15,6 @@ - @@ -24,4 +22,9 @@ + + + + + From f7428e1d7c3af8520d6d85c79ec4180dd32ae444 Mon Sep 17 00:00:00 2001 From: grant Date: Wed, 2 Dec 2015 16:31:09 +0800 Subject: [PATCH 5/8] fixed error operation --- Example/JSBadgeView/JSViewController.m | 6 +- Pod/Classes/JSBadgeView.h | 36 ++++++++ Pod/Classes/JSBadgeView.m | 111 ++++++++++++++++++++----- 3 files changed, 129 insertions(+), 24 deletions(-) diff --git a/Example/JSBadgeView/JSViewController.m b/Example/JSBadgeView/JSViewController.m index 24e2c76..42f70ab 100644 --- a/Example/JSBadgeView/JSViewController.m +++ b/Example/JSBadgeView/JSViewController.m @@ -20,7 +20,7 @@ #import -#define kNumBadges 100 +#define kNumBadges 120 #define kViewBackgroundColor [UIColor colorWithRed:0.357 green:0.757 blue:0.357 alpha:1] @@ -76,7 +76,9 @@ - (void)loadView JSBadgeView *badgeView = [[JSBadgeView alloc] initWithParentView:rectangle alignment:JSBadgeViewAlignmentTopRight]; badgeView.badgeText = [NSString stringWithFormat:@"%d", i]; - + if (i >= 100) { + badgeView.tinyMode = YES; + } [scrollView addSubview:rectangle]; [scrollView sendSubviewToBack:rectangle]; diff --git a/Pod/Classes/JSBadgeView.h b/Pod/Classes/JSBadgeView.h index 8128e9d..591aad2 100644 --- a/Pod/Classes/JSBadgeView.h +++ b/Pod/Classes/JSBadgeView.h @@ -92,6 +92,42 @@ typedef NS_ENUM(NSUInteger, JSBadgeViewAlignment) */ @property (nonatomic, assign) CGFloat badgeMinWidth UI_APPEARANCE_SELECTOR; +/** + * The margin add to the text left and right,both left and right use badgeViewTextSideMargin/2. + */ +@property (nonatomic,assign) CGFloat badgeViewTextSideMargin UI_APPEARANCE_SELECTOR; + +/** + * The corner radius use to the display rect. + */ +@property (nonatomic,assign) CGFloat badgeViewCornerRadius UI_APPEARANCE_SELECTOR; + +/** + * The height for the hold rect of text. + */ +@property (nonatomic,assign) CGFloat badgeViewHeight UI_APPEARANCE_SELECTOR; +/** + * shadow radius, if it > 0, we will draw a shadow. + */ +@property (nonatomic,assign) CGFloat badgeViewShadowRadius UI_APPEARANCE_SELECTOR; +/** + * will you prefer a backgroud image? it will auto scale to fit the badge size with the same radio. + */ +@property (nonatomic,strong) UIImage* badgeBackgroundImage UI_APPEARANCE_SELECTOR; +/** + * tiny mode,not show text,just a small cire. + */ +@property (nonatomic,assign) BOOL tinyMode UI_APPEARANCE_SELECTOR; +/** + * tiny mode's backgroud color. + */ +@property (nonatomic,strong) UIColor* tinyModeBackgroundColor UI_APPEARANCE_SELECTOR; + +/** + * tiny mode's radius. + */ +@property (nonatomic,assign) CGFloat badgeViewTinyRadius UI_APPEARANCE_SELECTOR; + /** * Optionally init using this method to have the badge automatically added to another view. */ diff --git a/Pod/Classes/JSBadgeView.m b/Pod/Classes/JSBadgeView.m index e3c5366..54136e3 100644 --- a/Pod/Classes/JSBadgeView.m +++ b/Pod/Classes/JSBadgeView.m @@ -78,6 +78,13 @@ + (void)applyCommonStyle badgeViewAppearanceProxy.badgeBackgroundColor = UIColor.redColor; badgeViewAppearanceProxy.badgeTextFont = [UIFont boldSystemFontOfSize:UIFont.systemFontSize]; badgeViewAppearanceProxy.badgeTextColor = UIColor.whiteColor; + badgeViewAppearanceProxy.badgeViewShadowRadius = 1.0f; + badgeViewAppearanceProxy.badgeViewHeight = 16.0f; + badgeViewAppearanceProxy.badgeViewTextSideMargin = 8.0f; + badgeViewAppearanceProxy.badgeViewCornerRadius = 10.0f; + badgeViewAppearanceProxy.tinyMode = NO; + badgeViewAppearanceProxy.tinyModeBackgroundColor = UIColor.redColor; + badgeViewAppearanceProxy.badgeViewTinyRadius = 5.0f; } + (void)applyLegacyStyle @@ -131,6 +138,21 @@ - (id)initWithParentView:(UIView *)parentView alignment:(JSBadgeViewAlignment)al return self; } +-(void) setTinyMode:(BOOL)tinyMode +{ + if ( tinyMode == self.tinyMode) { + return; + } + _tinyMode = tinyMode; + [self setNeedsLayout]; +} + +-(void)setBadgeBackgroundImage:(UIImage *)badgeBackgroundImage +{ + _badgeBackgroundImage = badgeBackgroundImage; + [self setNeedsLayout]; +} + #pragma mark - Layout - (CGFloat)marginToDrawInside @@ -141,19 +163,25 @@ - (CGFloat)marginToDrawInside - (void)layoutSubviews { [super layoutSubviews]; - + + CGRect newFrame = self.frame; - const CGRect superviewBounds = CGRectIsEmpty(_frameToPositionInRelationWith) ? self.superview.bounds : _frameToPositionInRelationWith; + const CGRect superviewBounds = CGRectIsEmpty(self.frameToPositionInRelationWith) ? self.superview.bounds : self.frameToPositionInRelationWith; const CGFloat textWidth = [self sizeOfTextForCurrentSettings].width; - + const CGFloat marginToDrawInside = [self marginToDrawInside]; - const CGFloat viewWidth = MAX(_badgeMinWidth, textWidth + JSBadgeViewTextSideMargin + (marginToDrawInside * 2)); - const CGFloat viewHeight = JSBadgeViewHeight + (marginToDrawInside * 2); + CGFloat viewWidth = MAX(self.badgeMinWidth, textWidth + self.badgeViewTextSideMargin + (marginToDrawInside * 2)); + CGFloat viewHeight = self.badgeViewHeight + (marginToDrawInside * 2); const CGFloat superviewWidth = superviewBounds.size.width; const CGFloat superviewHeight = superviewBounds.size.height; + if (self.tinyMode) { + viewWidth = 2*(self.badgeViewTinyRadius+self.badgeViewShadowRadius); + viewHeight = viewWidth; + } + newFrame.size.width = MAX(viewWidth, viewHeight); newFrame.size.height = viewHeight; @@ -348,19 +376,51 @@ - (void)drawRect:(CGRect)rect const CGFloat marginToDrawInside = [self marginToDrawInside]; const CGRect rectToDraw = CGRectInset(rect, marginToDrawInside, marginToDrawInside); - UIBezierPath *borderPath = [UIBezierPath bezierPathWithRoundedRect:rectToDraw byRoundingCorners:(UIRectCorner)UIRectCornerAllCorners cornerRadii:CGSizeMake(JSBadgeViewCornerRadius, JSBadgeViewCornerRadius)]; + UIBezierPath *borderPath; + if (self.tinyMode) { + borderPath= [UIBezierPath bezierPathWithRoundedRect:rectToDraw byRoundingCorners:(UIRectCorner)UIRectCornerAllCorners cornerRadii:CGSizeMake(self.badgeViewTinyRadius+marginToDrawInside, self.badgeViewTinyRadius+marginToDrawInside)]; + } + else{ + borderPath= [UIBezierPath bezierPathWithRoundedRect:rectToDraw byRoundingCorners:(UIRectCorner)UIRectCornerAllCorners cornerRadii:CGSizeMake(self.badgeViewCornerRadius, self.badgeViewCornerRadius)]; + } + CGColorRef bgCGColor = self.tinyMode ? self.tinyModeBackgroundColor.CGColor : self.badgeBackgroundColor.CGColor; - /* Background and shadow */ - CGContextSaveGState(ctx); - { - CGContextAddPath(ctx, borderPath.CGPath); + if (nil == self.badgeBackgroundImage) { + CGContextSetFillColorWithColor(ctx, bgCGColor); + /* Background and shadow */ + CGContextSaveGState(ctx); + { + CGContextAddPath(ctx, borderPath.CGPath); + if (!self.tinyMode && self.badgeViewShadowRadius > 0 ) { + CGContextSetShadowWithColor(ctx, self.badgeShadowSize, self.badgeViewShadowRadius, self.badgeShadowColor.CGColor); + } + + CGContextDrawPath(ctx, kCGPathFill); + } + CGContextRestoreGState(ctx); + + } + else{ + + CGFloat width = self.badgeBackgroundImage.size.width; + CGFloat height = self.badgeBackgroundImage.size.height; + + float verticalRadio = rectToDraw.size.height*1.0/height; + float horizontalRadio = rectToDraw.size.width*1.0/width; + + float radio = 1; + radio = verticalRadio < horizontalRadio ? horizontalRadio : verticalRadio; - CGContextSetFillColorWithColor(ctx, self.badgeBackgroundColor.CGColor); - CGContextSetShadowWithColor(ctx, self.badgeShadowSize, JSBadgeViewShadowRadius, self.badgeShadowColor.CGColor); + CGRect displayRect = CGRectMake((rectToDraw.size.width - width*radio)/2.0f, + (rectToDraw.size.height-height*radio)/2.0f, + width*radio, + height*radio); + + [self.badgeBackgroundImage drawInRect:displayRect]; - CGContextDrawPath(ctx, kCGPathFill); } - CGContextRestoreGState(ctx); + + const BOOL colorForOverlayPresent = self.badgeOverlayColor && ![self.badgeOverlayColor isEqual:[UIColor clearColor]]; @@ -400,25 +460,32 @@ - (void)drawRect:(CGRect)rect } CGContextRestoreGState(ctx); + if (self.tinyMode) { + return; + } /* Text */ CGContextSaveGState(ctx); { CGContextSetFillColorWithColor(ctx, self.badgeTextColor.CGColor); - CGContextSetShadowWithColor(ctx, self.badgeTextShadowOffset, 1.0, self.badgeTextShadowColor.CGColor); + if (self.badgeViewShadowRadius > 0) { + CGContextSetShadowWithColor(ctx, self.badgeTextShadowOffset, 1.0, self.badgeTextShadowColor.CGColor); + } CGRect textFrame = rectToDraw; const CGSize textSize = [self sizeOfTextForCurrentSettings]; textFrame.size.height = textSize.height; - textFrame.origin.y = rectToDraw.origin.y + floorf((rectToDraw.size.height - textFrame.size.height) / 2.0f); - - JSBadgeViewSilenceDeprecatedMethodStart(); + textFrame.origin.y = rectToDraw.origin.y + ceilf((rectToDraw.size.height - textFrame.size.height) / 2.0f); + + NSMutableParagraphStyle *textStyle = [[NSMutableParagraphStyle defaultParagraphStyle] mutableCopy]; + textStyle.lineBreakMode = NSLineBreakByClipping; + textStyle.alignment = NSTextAlignmentCenter; + [self.badgeText drawInRect:textFrame - withFont:self.badgeTextFont - lineBreakMode:NSLineBreakByClipping - alignment:NSTextAlignmentCenter]; - JSBadgeViewSilenceDeprecatedMethodEnd(); + withAttributes:@{NSFontAttributeName:self.badgeTextFont, + NSParagraphStyleAttributeName: textStyle}]; + } CGContextRestoreGState(ctx); } From 5fbe0b09302fd33d88b935de7b266dd6f84ab73b Mon Sep 17 00:00:00 2001 From: grant Date: Wed, 2 Dec 2015 16:33:13 +0800 Subject: [PATCH 6/8] fixed how shadow draw --- Pod/Classes/JSBadgeView.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Pod/Classes/JSBadgeView.m b/Pod/Classes/JSBadgeView.m index 54136e3..7c04e97 100644 --- a/Pod/Classes/JSBadgeView.m +++ b/Pod/Classes/JSBadgeView.m @@ -178,7 +178,7 @@ - (void)layoutSubviews const CGFloat superviewHeight = superviewBounds.size.height; if (self.tinyMode) { - viewWidth = 2*(self.badgeViewTinyRadius+self.badgeViewShadowRadius); + viewWidth = 2*self.badgeViewTinyRadius; viewHeight = viewWidth; } From 0e47e8a261b30c0bc52573f6ee5ffad23ee732b9 Mon Sep 17 00:00:00 2001 From: "grant.zhou" Date: Wed, 16 Jan 2019 10:21:06 +0800 Subject: [PATCH 7/8] fix tiny mode --- Pod/Classes/JSBadgeView.m | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/Pod/Classes/JSBadgeView.m b/Pod/Classes/JSBadgeView.m index 673ea1c..67a0485 100644 --- a/Pod/Classes/JSBadgeView.m +++ b/Pod/Classes/JSBadgeView.m @@ -63,6 +63,15 @@ + (void)applyStyle badgeViewAppearanceProxy.badgeShadowColor = UIColor.clearColor; badgeViewAppearanceProxy.badgeStrokeWidth = 0.0f; badgeViewAppearanceProxy.badgeStrokeColor = badgeViewAppearanceProxy.badgeBackgroundColor; + + badgeViewAppearanceProxy.badgeViewShadowRadius = 1.0f; + badgeViewAppearanceProxy.badgeViewHeight = 16.0f; + badgeViewAppearanceProxy.badgeViewTextSideMargin = 8.0f; + badgeViewAppearanceProxy.badgeViewCornerRadius = 10.0f; + badgeViewAppearanceProxy.tinyMode = NO; + badgeViewAppearanceProxy.tinyModeBackgroundColor = UIColor.redColor; + badgeViewAppearanceProxy.badgeViewTinyRadius = 5.0f; + } + (void)initialize From 5d3f62e2c10036358e417a2bb167a471d22d3360 Mon Sep 17 00:00:00 2001 From: "grant.zhou" Date: Wed, 16 Jan 2019 15:34:19 +0800 Subject: [PATCH 8/8] update podfile.lock --- Example/Podfile.lock | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Example/Podfile.lock b/Example/Podfile.lock index 42ed2da..3144819 100644 --- a/Example/Podfile.lock +++ b/Example/Podfile.lock @@ -1,6 +1,6 @@ PODS: - Expecta (1.0.6) - - JSBadgeView (1.4.1) + - JSBadgeView (2.0.0) - Specta (1.0.7) DEPENDENCIES: @@ -9,7 +9,7 @@ DEPENDENCIES: - Specta SPEC REPOS: - https://github.com/CocoaPods/Specs.git: + https://github.com/cocoapods/specs.git: - Expecta - Specta @@ -19,9 +19,9 @@ EXTERNAL SOURCES: SPEC CHECKSUMS: Expecta: 3b6bd90a64b9a1dcb0b70aa0e10a7f8f631667d5 - JSBadgeView: 67b15e14438b19adc40ddc016d36b07016a68044 + JSBadgeView: 3dbfbaf6c580f5f8dc8d1283d826c94f2041b7c5 Specta: 3e1bd89c3517421982dc4d1c992503e48bd5fe66 PODFILE CHECKSUM: c814e0cbf7cb16d7dab3042bf5dc39c9d36b0d13 -COCOAPODS: 1.5.0 +COCOAPODS: 1.5.3