-
Notifications
You must be signed in to change notification settings - Fork 34
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Badge text support #8
base: master
Are you sure you want to change the base?
Changes from 3 commits
7f66659
ba29a06
8054a5c
9c309d8
69caaec
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,19 @@ | |
#import "UIView+MGBadgeView.h" | ||
#import <objc/runtime.h> | ||
|
||
void MGFillRoundedRect(CGContextRef __nullable context, CGRect rect, CGFloat radius) { | ||
CGFloat theRadius = MAX(radius, rect.size.height/2.0); | ||
CGFloat minx = CGRectGetMinX(rect), midx = CGRectGetMidX(rect), maxx = CGRectGetMaxX(rect); | ||
CGFloat miny = CGRectGetMinY(rect), midy = CGRectGetMidY(rect), maxy = CGRectGetMaxY(rect); | ||
CGContextMoveToPoint(context, minx, midy); | ||
CGContextAddArcToPoint(context, minx, miny, midx, miny, theRadius); | ||
CGContextAddArcToPoint(context, maxx, miny, maxx, midy, theRadius); | ||
CGContextAddArcToPoint(context, maxx, maxy, midx, maxy, theRadius); | ||
CGContextAddArcToPoint(context, minx, maxy, minx, midy, theRadius); | ||
CGContextClosePath(context); | ||
CGContextFillPath(context); | ||
} | ||
|
||
@implementation MGBadgeView | ||
|
||
static float const kMGBadgeViewInnerSpaceFromBorder = 7.0; | ||
|
@@ -42,19 +55,29 @@ - (instancetype)initWithFrame:(CGRect)frame { | |
} | ||
|
||
- (void)drawRect:(CGRect)rect { | ||
NSString *stringToDraw = nil; | ||
if(_badgeImage) { | ||
stringToDraw = nil; | ||
} else if(_badgeText) { | ||
stringToDraw = _badgeText; | ||
} else if(_badgeValue != 0 || _displayIfZero) { | ||
stringToDraw = [NSString stringWithFormat:@"%ld", (long)_badgeValue]; | ||
} | ||
|
||
if(_badgeValue != 0 || _displayIfZero) { | ||
|
||
NSString *stringToDraw = [NSString stringWithFormat:@"%ld", (long)_badgeValue]; | ||
|
||
CGContextRef context = UIGraphicsGetCurrentContext(); | ||
|
||
[_outlineColor set]; | ||
CGContextFillEllipseInRect(context, CGRectInset(rect, 1.0, 1.0)); | ||
|
||
[_badgeColor set]; | ||
CGContextFillEllipseInRect(context, CGRectInset(rect, _outlineWidth + 1.0, _outlineWidth + 1.0)); | ||
|
||
CGContextRef context = UIGraphicsGetCurrentContext(); | ||
|
||
[_outlineColor set]; | ||
MGFillRoundedRect(context, CGRectInset(rect, 1.0, 1.0), 0); | ||
|
||
[_badgeColor set]; | ||
MGFillRoundedRect(context, CGRectInset(rect, _outlineWidth + 1.0, _outlineWidth + 1.0), 0); | ||
|
||
if(_badgeImage) { | ||
CGRect imageRect = CGRectMake((rect.size.width - _badgeImage.size.width)/2.0, (rect.size.height - _badgeImage.size.height)/2.0, _badgeImage.size.width, _badgeImage.size.height); | ||
[_badgeImage drawInRect:imageRect]; | ||
} | ||
|
||
if(stringToDraw) { | ||
CGSize numberSize = [stringToDraw sizeWithAttributes:@{NSFontAttributeName: _font}]; | ||
|
||
[_textColor set]; | ||
|
@@ -69,17 +92,16 @@ - (void)drawRect:(CGRect)rect { | |
NSParagraphStyleAttributeName : paragrapStyle, | ||
NSForegroundColorAttributeName : _textColor | ||
}]; | ||
|
||
} | ||
} | ||
|
||
#pragma mark - Properties accessor methods | ||
|
||
- (void)setBadgeValue:(NSInteger)badgeValue { | ||
|
||
if(_badgeValue != badgeValue) { | ||
|
||
_badgeValue = badgeValue; | ||
_badgeText = nil; | ||
_badgeImage = nil; | ||
|
||
if(badgeValue != 0 || _displayIfZero) { | ||
[self mg_updateBadgeViewSize]; | ||
|
@@ -95,6 +117,43 @@ - (void)setBadgeValue:(NSInteger)badgeValue { | |
} | ||
} | ||
|
||
- (void)setBadgeText:(NSString *)badgeText { | ||
if(![(badgeText ?: @"") isEqualToString:(_badgeText ?: @"")]) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. would be nicer if you can bring this in a separated method, maybe a static function like MGEqualObjects(obj1, obj2) which checks nullability. Or otherwise use the normal isEqualToString and before that, just check if they are both nil, and in that case return. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok, I've simplified it to simply
And tested, it has the same result. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No dude sorry, even if they are NSString, I don't like to compare objects in this way. I gave you a suggest on how to approach, I don't like shortcut :)
|
||
_badgeText = [NSString stringWithString:(badgeText ?: @"")]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. so we can maybe also improve this line making assumptions before. |
||
_badgeValue = 0; | ||
_badgeImage = nil; | ||
|
||
if(_badgeText.length > 0) { | ||
[self mg_updateBadgeViewSize]; | ||
if(_position == MGBadgePositionBest) | ||
[self mg_updateBadgeViewPosition]; | ||
} else { | ||
self.frame = CGRectZero; | ||
} | ||
|
||
[self setNeedsDisplay]; | ||
} | ||
} | ||
|
||
- (void)setBadgeImage:(UIImage *)badgeImage { | ||
if(_badgeImage != badgeImage) { | ||
_badgeImage = badgeImage; | ||
_badgeText = nil; | ||
_badgeValue = 0; | ||
|
||
if(badgeImage) { | ||
[self mg_updateBadgeViewSize]; | ||
if(_position == MGBadgePositionBest) { | ||
[self mg_updateBadgeViewPosition]; | ||
} | ||
} else { | ||
self.frame = CGRectZero; | ||
} | ||
|
||
[self setNeedsDisplay]; | ||
} | ||
} | ||
|
||
- (void)setPosition:(MGBadgePosition)position { | ||
if(_position != position) { | ||
_position = position; | ||
|
@@ -181,15 +240,23 @@ - (void)setDisplayIfZero:(BOOL)displayIfZero { | |
|
||
- (void)mg_updateBadgeViewSize { | ||
//Calculate badge bounds | ||
CGSize numberSize = [[NSString stringWithFormat:@"%ld", (long)_badgeValue] sizeWithAttributes:@{NSFontAttributeName: _font}]; | ||
|
||
float badgeHeight = MAX(BADGE_TOTAL_OFFSET + numberSize.height, _minDiameter); | ||
float badgeWidth = MAX(badgeHeight, BADGE_TOTAL_OFFSET + numberSize.width); | ||
CGSize contentSize = CGSizeZero; | ||
if (_badgeImage) { | ||
// assume images should always be rendered in a circle, hence ensure content is square for equal padding | ||
CGFloat imageLongestSide = MAX(_badgeImage.size.width, _badgeImage.size.height); | ||
contentSize = CGSizeMake(imageLongestSide, imageLongestSide); | ||
} else if(_badgeText) { | ||
contentSize = [(_badgeText ?: @"") sizeWithAttributes:@{NSFontAttributeName: _font}]; | ||
} else { | ||
contentSize = [[NSString stringWithFormat:@"%ld", (long)_badgeValue] sizeWithAttributes:@{NSFontAttributeName: _font}]; | ||
} | ||
|
||
float badgeHeight = ceilf(MAX(BADGE_TOTAL_OFFSET + contentSize.height, _minDiameter)); | ||
float badgeWidth = ceilf(MAX(badgeHeight, BADGE_TOTAL_OFFSET + contentSize.width)); | ||
|
||
[self setBounds:CGRectMake(0, 0, badgeWidth, badgeHeight)]; | ||
} | ||
|
||
|
||
- (void)mg_updateBadgeViewPosition { | ||
CGRect superviewFrame = self.superview.frame; | ||
CGSize badgeSize = self.bounds.size; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you transform:
in
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will do 👍