Skip to content
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

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions UIView+MGBadgeView.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ typedef NS_ENUM(NSUInteger, MGBadgePosition) {

@property (copy, nonatomic) NSString *badgeText;

@property (strong, nonatomic) UIImage *badgeImage;

@property(strong, nonatomic) UIFont *font;

@property(strong, nonatomic) UIColor *badgeColor;
Expand Down
78 changes: 51 additions & 27 deletions UIView+MGBadgeView.m
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,6 @@ void MGFillRoundedRect(CGContextRef __nullable context, CGRect rect, CGFloat rad
CGContextFillPath(context);
}

@interface MGBadgeView ()
@property (nonatomic) BOOL useText;
@end

@implementation MGBadgeView

static float const kMGBadgeViewInnerSpaceFromBorder = 7.0;
Expand Down Expand Up @@ -59,20 +55,29 @@ - (instancetype)initWithFrame:(CGRect)frame {
}

- (void)drawRect:(CGRect)rect {
NSString *stringToDraw = nil;
if(_badgeImage) {
stringToDraw = nil;
} else if(_badgeText) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you transform:

if(_badgeImage) {
         stringToDraw = nil;
} else if(_badgeText) {

in

if(!_badgeImage) {
     if(_badgeText) {
         .....

?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will do 👍

stringToDraw = _badgeText;
} else if(_badgeValue != 0 || _displayIfZero) {
stringToDraw = [NSString stringWithFormat:@"%ld", (long)_badgeValue];
}

if ((_useText && (_badgeText ?: @"").length > 0) || (!_useText && (_badgeValue != 0 || _displayIfZero))) {

NSString *stringToDraw = _useText ? (_badgeText ?: @"") : [NSString stringWithFormat:@"%ld", (long)_badgeValue];

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);

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];
Expand All @@ -87,19 +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;
_useText = NO;
_badgeImage = nil;

if(badgeValue != 0 || _displayIfZero) {
[self mg_updateBadgeViewSize];
Expand All @@ -119,7 +121,7 @@ - (void)setBadgeText:(NSString *)badgeText {
if(![(badgeText ?: @"") isEqualToString:(_badgeText ?: @"")]) {
Copy link
Owner

Choose a reason for hiding this comment

The 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.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I've simplified it to simply

    if(_badgeText != badgeText) {
        _badgeText = badgeText;

And tested, it has the same result.

Copy link
Owner

Choose a reason for hiding this comment

The 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 :)

static BOOL MGEqualObjects(NSString *string1, NSString *string2) {
    return (!string1 && !string2) || [string1 isEqualToString:string2];
}

_badgeText = [NSString stringWithString:(badgeText ?: @"")];
Copy link
Owner

Choose a reason for hiding this comment

The 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;
_useText = YES;
_badgeImage = nil;

if(_badgeText.length > 0) {
[self mg_updateBadgeViewSize];
Expand All @@ -133,6 +135,25 @@ - (void)setBadgeText:(NSString *)badgeText {
}
}

- (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;
Expand Down Expand Up @@ -220,19 +241,22 @@ - (void)setDisplayIfZero:(BOOL)displayIfZero {
- (void)mg_updateBadgeViewSize {
//Calculate badge bounds
CGSize contentSize = CGSizeZero;
if (_useText) {
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 = MAX(BADGE_TOTAL_OFFSET + contentSize.height, _minDiameter);
float badgeWidth = MAX(badgeHeight, BADGE_TOTAL_OFFSET + contentSize.width);
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;
Expand Down