Permalink
Browse files

Initial checkin

  • Loading branch information...
Robert Rasmussen
Robert Rasmussen committed Oct 3, 2010
0 parents commit 4417782701054dcf4ee0a596d7461a63f90f3038
@@ -0,0 +1,22 @@
+//
+// MLTBadgedViewAppDelegate.h
+// MLTBadgedView
+//
+// Created by Robert Rasmussen on 10/2/10.
+// Copyright 2010 Moonlight Tower. All rights reserved.
+//
+
+#import <UIKit/UIKit.h>
+
+@class MLTBadgedViewViewController;
+
+@interface MLTBadgedViewAppDelegate : NSObject <UIApplicationDelegate> {
+ UIWindow *window;
+ MLTBadgedViewViewController *viewController;
+}
+
+@property (nonatomic, retain) IBOutlet UIWindow *window;
+@property (nonatomic, retain) IBOutlet MLTBadgedViewViewController *viewController;
+
+@end
+
@@ -0,0 +1,88 @@
+//
+// MLTBadgedViewAppDelegate.m
+// MLTBadgedView
+//
+// Created by Robert Rasmussen on 10/2/10.
+// Copyright 2010 Moonlight Tower. All rights reserved.
+//
+
+#import "MLTBadgedViewAppDelegate.h"
+#import "MLTBadgedViewViewController.h"
+
+@implementation MLTBadgedViewAppDelegate
+
+@synthesize window;
+@synthesize viewController;
+
+
+#pragma mark -
+#pragma mark Application lifecycle
+
+- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
+
+ // Override point for customization after application launch.
+
+ // Add the view controller's view to the window and display.
+ [window addSubview:viewController.view];
+ [window makeKeyAndVisible];
+
+ return YES;
+}
+
+
+- (void)applicationWillResignActive:(UIApplication *)application {
+ /*
+ Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
+ Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
+ */
+}
+
+
+- (void)applicationDidEnterBackground:(UIApplication *)application {
+ /*
+ Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
+ If your application supports background execution, called instead of applicationWillTerminate: when the user quits.
+ */
+}
+
+
+- (void)applicationWillEnterForeground:(UIApplication *)application {
+ /*
+ Called as part of transition from the background to the inactive state: here you can undo many of the changes made on entering the background.
+ */
+}
+
+
+- (void)applicationDidBecomeActive:(UIApplication *)application {
+ /*
+ Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
+ */
+}
+
+
+- (void)applicationWillTerminate:(UIApplication *)application {
+ /*
+ Called when the application is about to terminate.
+ See also applicationDidEnterBackground:.
+ */
+}
+
+
+#pragma mark -
+#pragma mark Memory management
+
+- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application {
+ /*
+ Free up as much memory as possible by purging cached data objects that can be recreated (or reloaded from disk) later.
+ */
+}
+
+
+- (void)dealloc {
+ [viewController release];
+ [window release];
+ [super dealloc];
+}
+
+
+@end
@@ -0,0 +1,16 @@
+//
+// MLTBadgedViewViewController.h
+// MLTBadgedView
+//
+// Created by Robert Rasmussen on 10/2/10.
+// Copyright 2010 Moonlight Tower. All rights reserved.
+//
+
+#import <UIKit/UIKit.h>
+
+@interface MLTBadgedViewViewController : UIViewController {
+
+}
+
+@end
+
@@ -0,0 +1,78 @@
+//
+// MLTBadgedViewViewController.m
+// MLTBadgedView
+//
+// Created by Robert Rasmussen on 10/2/10.
+// Copyright 2010 Moonlight Tower. All rights reserved.
+//
+
+#import "MLTBadgedViewViewController.h"
+#import "UIView+Badge.h"
+
+@implementation MLTBadgedViewViewController
+
+//Examples of how to use UIView+Badge
+- (void)viewDidLoad {
+ [super viewDidLoad];
+ self.view.backgroundColor = [UIColor lightGrayColor];
+
+ // Button placed flush right will be badged on the left by default
+ UIButton *b = [UIButton buttonWithType:UIButtonTypeRoundedRect];
+ b.frame = CGRectMake(120,50,200,50);
+ [b setTitle:@"Auto Badge Placement" forState:UIControlStateNormal];
+ b.enabled = YES;
+ b.badge.outlineWidth = 2.0;
+ b.badge.badgeValue = 47;
+ b.badge.badgeColor = [UIColor orangeColor];
+ [self.view addSubview:b];
+
+ // Here we force the badge to the left side, and make the outline wide
+ UIButton *b2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
+ b2.frame = CGRectMake(100,120,200,50);
+ [b2 setTitle:@"Wide Badge Outline" forState:UIControlStateNormal];
+ b2.badge.outlineWidth = 5.0;
+ b2.badge.placement = kBadgePlacementUpperLeft;
+ b2.badge.badgeValue = 47;
+ [self.view addSubview:b2];
+
+ // Huge badge with custom colors
+ UIButton *b3 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
+ b3.frame = CGRectMake(100, 200, 200, 50);
+ [b3 setTitle:@"Ugly Badge Support" forState:UIControlStateNormal];
+ b3.badge.outlineWidth = 5.0;
+ b3.badge.outlineColor = [UIColor blackColor];
+ b3.badge.badgeColor = [UIColor brownColor];
+ b3.badge.textColor = [UIColor purpleColor];
+ b3.badge.minimumDiameter = 60.0;
+ b3.badge.font = [UIFont boldSystemFontOfSize:20];
+ b3.badge.badgeValue = 50;
+ [self.view addSubview:b3];
+
+ // Auto placement will badge to the upper right if possible
+ UIButton *b4 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
+ b4.frame = CGRectMake(80,300,200,50);
+ [b4 setTitle:@"Auto Badge Placement" forState:UIControlStateNormal];
+ b4.enabled = YES;
+ b4.badge.outlineWidth = 1.0;
+ b4.badge.outlineColor = [UIColor redColor];
+ b4.badge.badgeValue = 3;
+ b4.badge.badgeColor = [UIColor darkGrayColor];
+ [self.view addSubview:b4];
+
+ // Any view can be badged
+ UIView *b5 = [[UIView alloc] initWithFrame:CGRectMake(80, 370, 200, 40)];
+ b5.backgroundColor = [UIColor blackColor];
+ UILabel *b5l = [[UILabel alloc]init];
+ b5l.font = [UIFont boldSystemFontOfSize:15.0];
+ b5l.text = @"Badged UIView";
+ [b5l sizeToFit];
+ b5l.backgroundColor = [UIColor redColor];
+ b5l.textColor = [UIColor whiteColor];
+ b5l.center = CGPointMake(100, 20);
+ [b5 addSubview:[b5l autorelease]];
+
+ b5.badge.badgeValue = 44;
+ [self.view addSubview:[b5 autorelease]];
+}
+
+@end
@@ -0,0 +1,46 @@
+//
+// UIView+Badge.h
+// MLTBadgedView
+//
+// Created by Robert Rasmussen on 10/2/10.
+// Copyright 2010 Moonlight Tower. All rights reserved.
+//
+
+#import <UIKit/UIKit.h>
+
+typedef enum {
+ kBadgePlacementUpperLeft,
+ kBadgePlacementUpperRight,
+ kBadgePlacementUpperBest
+} MLTBadgePlacement;
+
+
+@interface MLTBadgeView : UIView {
+
+}
+// Determines where badge is placed
+@property MLTBadgePlacement placement;
+// The numeric value to display on the badge
+@property NSInteger badgeValue;
+// The font for the badge number
+@property(nonatomic, retain) UIFont *font;
+// The interior color of the badge
+@property(nonatomic, retain) UIColor *badgeColor;
+// The color for badge text
+@property(nonatomic, retain) UIColor *textColor;
+// The outline color for the badge
+@property(nonatomic, retain) UIColor *outlineColor;
+// The width of the outline around the badge
+@property float outlineWidth;
+// Force the badge to a minimum size
+@property float minimumDiameter;
+// Show the badge no matter what
+@property BOOL displayWhenZero;
+@end
+
+// Addition to UIView which allows any UIView to display
+// a badge in the upper left or upper right corner of
+// the view.
+@interface UIView(Badged)
+@property(nonatomic, readonly) MLTBadgeView *badge;
+@end
@@ -0,0 +1,105 @@
+//
+// UIView+Badge.m
+// MLTBadgedView
+//
+// Created by Robert Rasmussen on 10/2/10.
+// Copyright 2010 Moonlight Tower. All rights reserved.
+//
+
+#import "UIView+Badge.h"
+
+static int MLT_BADGE_TAG = 6546;
+
+@implementation MLTBadgeView
+@synthesize placement, badgeValue, font, badgeColor, textColor, outlineColor, outlineWidth, minimumDiameter, displayWhenZero;
+
+-(void)dealloc {
+ self.font = nil;
+ self.badgeColor = nil;
+ self.textColor = nil;
+ self.outlineColor = nil;
+ [super dealloc];
+}
+
+-(id)initWithFrame:(CGRect)frame {
+ if(self = [super initWithFrame:frame]) {
+ self.font = [UIFont boldSystemFontOfSize:13.0];
+ self.badgeColor = [UIColor redColor];
+ self.textColor = [UIColor whiteColor];
+ self.outlineColor = [UIColor whiteColor];
+ self.outlineWidth = 2.0;
+ self.backgroundColor = [UIColor clearColor];
+ self.minimumDiameter = 20.0;
+ self.placement = kBadgePlacementUpperBest;
+ self.opaque = YES;
+ }
+ return self;
+}
+-(void)setBadgeValue:(NSInteger)value {
+ if(value != 0 || self.displayWhenZero) {
+ CGSize numberSize = [[NSString stringWithFormat:@"%d", value] sizeWithFont:self.font];
+ float diameterForNumber = numberSize.width > numberSize.height ? numberSize.width : numberSize.height;
+ float diameter = diameterForNumber + 6 + (self.outlineWidth * 2);
+ if(diameter < self.minimumDiameter) {
+ diameter = self.minimumDiameter;
+ }
+
+ //We know the size of the badge circle. If no explicit placement for the badge has been set, we'll
+ //see if it works on the right side first.
+ CGRect superviewFrame = self.superview.frame;
+ if(self.placement == kBadgePlacementUpperBest) {
+ CGPoint rightMostInWindow = [self.superview convertPoint:CGPointMake(superviewFrame.origin.x + superviewFrame.size.width + (diameter / 2.0), -(diameter / 2.0)) fromView:nil];
+ if(rightMostInWindow.x > [[UIScreen mainScreen] applicationFrame].size.width) {
+ self.placement = kBadgePlacementUpperLeft;
+ } else {
+ self.placement = kBadgePlacementUpperRight;
+ }
+ }
+ self.bounds = CGRectMake(0, 0, diameter, diameter);
+ self.center = (self.placement == kBadgePlacementUpperLeft) ? CGPointMake(0, 0) : CGPointMake(superviewFrame.size.width, 0);
+ } else {
+ self.frame = CGRectZero;
+ }
+ badgeValue = value;
+ [self setNeedsDisplay];
+}
+-(void)setMinimumDiameter:(float)f {
+ minimumDiameter = f;
+ self.bounds = CGRectMake(0, 0, f, f);
+}
+
+-(void)drawRect:(CGRect)rect {
+ if(self.badgeValue != 0 || self.displayWhenZero) {
+ CGContextRef context = UIGraphicsGetCurrentContext();
+ [self.outlineColor set];
+ CGContextFillEllipseInRect(context, CGRectInset(rect, 1, 1));
+ [self.badgeColor set];
+ CGContextFillEllipseInRect(context, CGRectInset(rect, self.outlineWidth + 1, self.outlineWidth + 1));
+ [self.textColor set];
+ CGSize numberSize = [[NSString stringWithFormat:@"%d", self.badgeValue] sizeWithFont:self.font];
+ [[NSString stringWithFormat:@"%d", self.badgeValue] drawInRect:CGRectMake(self.outlineWidth + 3, (rect.size.height / 2.0) - (numberSize.height / 2.0), rect.size.width - (self.outlineWidth * 2) - 6, numberSize.height) withFont:self.font lineBreakMode:UILineBreakModeClip alignment:UITextAlignmentCenter];
+ }
+}
+
+@end
+
+
+@implementation UIView(Badged)
+
+-(MLTBadgeView *)badge {
+ UIView *existingView = [self viewWithTag:MLT_BADGE_TAG];
+ if(existingView) {
+ if(![existingView isKindOfClass:[MLTBadgeView class]]) {
+ NSLog(@"Unexpected view of class %@ found with badge tag.");
+ return nil;
+ } else {
+ return (MLTBadgeView *)existingView;
+ }
+ }
+ MLTBadgeView *badgeView = [[MLTBadgeView alloc]initWithFrame:CGRectZero];
+ badgeView.tag = MLT_BADGE_TAG;
+ [self addSubview:badgeView];
+ return [badgeView autorelease];
+}
+
+@end
@@ -0,0 +1,20 @@
+Copyright (c) 2010 Moonlight Tower, LLC, http://moonlighttower.com
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Oops, something went wrong.

0 comments on commit 4417782

Please sign in to comment.