Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@kerrishotts
Created February 18, 2013 20:13
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kerrishotts/4980294 to your computer and use it in GitHub Desktop.
Save kerrishotts/4980294 to your computer and use it in GitHub Desktop.
UIBarButtonItem Utility: Quick & Dirty bar button item creation using Font Awesome, Text Attributes, and Background images.
// Author: Kerri Shotts
// MIT LIcense
#import <UIKit/UIKit.h>
@interface UIBarButtonItem (Utility)
+(UIBarButtonItem *) barButtonItemUsingFontAwesomeIcon: (NSString *)iconKey
target: (id)theTarget
action: (SEL)theAction
withTitleTextAttributes: (NSDictionary *)textAttributes
andBackgroundImage: (UIImage *)theImage;
+(UIBarButtonItem *) barButtonItemWithTitle: (NSString *)theTitle
target: (id)theTarget
action: (SEL)theAction
withTitleTextAttributes: (NSDictionary *)textAttributes
andBackgroundImage: (UIImage *)theImage;
@end
// Author: Kerri Shotts
// MIT LIcense
#import "UIBarButtonItem+Utility.h"
#import "NSString+FontAwesome.h"
@implementation UIBarButtonItem (Utility)
+(UIBarButtonItem *) barButtonItemUsingFontAwesomeIcon: (NSString *)iconKey
target: (id)theTarget
action: (SEL)theAction
withTitleTextAttributes: (NSDictionary *)textAttributes
andBackgroundImage: (UIImage *)theImage
{
UIBarButtonItem *b = [[UIBarButtonItem alloc] initWithTitle:[NSString fontAwesomeIconStringForIconIdentifier: iconKey]
style:UIBarButtonItemStylePlain target:theTarget action:theAction];
[b setTitleTextAttributes:textAttributes forState:UIControlStateNormal];
[b setBackgroundImage:theImage forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
return b;
}
+(UIBarButtonItem *) barButtonItemWithTitle: (NSString *)theTitle
target: (id)theTarget
action: (SEL)theAction
withTitleTextAttributes: (NSDictionary *)textAttributes
andBackgroundImage: (UIImage *)theImage
{
UIBarButtonItem *b = [[UIBarButtonItem alloc] initWithTitle:theTitle style:UIBarButtonItemStylePlain target:theTarget action:theAction];
[b setTitleTextAttributes:textAttributes forState:UIControlStateNormal];
[b setBackgroundImage:theImage forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
return b;
}
@end
You can use it like so:
#import "NSString+FontAwesome.h"
...
NSDictionary *faTextAttributes = @{ UITextAttributeFont : [UIFont fontWithName: kFontAwesomeFamilyName size: 22],
UITextAttributeTextColor : [UIColor whiteColor],
UITextAttributeTextShadowColor: [UIColor blackColor],
UITextAttributeTextShadowOffset: [NSValue valueWithCGSize: CGSizeMake(0,-1)] };
NSDictionary *largeTextAttributes = @{ UITextAttributeFont : [UIFont fontWithName:@"Helvetica-Bold" size:20],
UITextAttributeTextColor : [UIColor whiteColor],
UITextAttributeTextShadowColor: [UIColor blackColor],
UITextAttributeTextShadowOffset: [NSValue valueWithCGSize: CGSizeMake(0,-1)] };
UIImage *blankImage = [UIImage new];
UIBarButtonItem *aMenuButton = [UIBarButtonItem barButtonItemUsingFontAwesomeIcon:@"icon-reorder"
target:self action:@selector(showMenu:)
withTitleTextAttributes:faTextAttributes
andBackgroundImage:blankImage];
UIBarButtonItem *aTextButton = [UIBarButtonItem barButtonItemWithTitle:@"M"
target:self action:@selector(doSomething:)
withTitleTextAttributes:largeTextAttributes
andBackgroundImage:blankImage];
You must have iOS FontAwesome from https://github.com/alexdrone/ios-fontawesome and the Font Awesome font itself added to your project.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment