-
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?
Conversation
Hi @matteogobbi was wondering if you would eventually accept this PR? I am looking at adding image support to the badges as well, and will most likely continue from this branch to keep text support as well. |
OK so I have added image support onto the same branch, so for better or worse it's also on this PR. If you would like one of the two features only (text or images) let us know and we will split the functionality and make a new PR. |
UIView+MGBadgeView.m
Outdated
NSString *stringToDraw = nil; | ||
if(_badgeImage) { | ||
stringToDraw = nil; | ||
} else if(_badgeText) { |
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:
if(_badgeImage) {
stringToDraw = nil;
} else if(_badgeText) {
in
if(!_badgeImage) {
if(_badgeText) {
.....
?
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 👍
UIView+MGBadgeView.m
Outdated
@@ -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 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.
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.
Ok, I've simplified it to simply
if(_badgeText != badgeText) {
_badgeText = badgeText;
And tested, it has the same result.
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.
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];
}
UIView+MGBadgeView.m
Outdated
@@ -95,6 +117,43 @@ - (void)setBadgeValue:(NSInteger)badgeValue { | |||
} | |||
} | |||
|
|||
- (void)setBadgeText:(NSString *)badgeText { | |||
if(![(badgeText ?: @"") isEqualToString:(_badgeText ?: @"")]) { | |||
_badgeText = [NSString stringWithString:(badgeText ?: @"")]; |
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.
so we can maybe also improve this line making assumptions before.
Hi @matteogobbi I have made the requested changes, ok to merge now? |
Adds support for text to badges (not just NSInteger).
Changes ellipse to a "pill" to support wider text.