Create a gist now

Instantly share code, notes, and snippets.

Method to add a title to an iPad silver-style UIToolbar, implemented as a category on UIBarButtonItem
//
// UIBarButtonItem+JFAdditions.h
//
// Created by Jacques Fortier on 1/10/11.
// Copyright 2011 Jacques Fortier.
// Released under the WTFPL (see LICENSE or http://sam.zoy.org/wtfpl/COPYING)
#import <UIKit/UIKit.h>
@interface UIBarButtonItem(JFAdditions)
- (id)initWithTitle:(NSString *)title width:(CGFloat)width;
@end
//
// UIBarButtonItem+JFAdditions.m
//
// Created by Jacques Fortier on 1/10/11.
// Copyright 2011 Jacques Fortier.
// Released under the WTFPL (see LICENSE or http://sam.zoy.org/wtfpl/COPYING)
#import "UIBarButtonItem+JFAdditions.h"
@implementation UIBarButtonItem(JFAdditions)
- (id)initWithTitle:(NSString *)title width:(CGFloat)width {
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, width, 23)];
label.backgroundColor = [UIColor clearColor];
label.opaque = NO;
label.textColor = [UIColor colorWithRed:0x71/255.0 green:0x78/255.0 blue:0x80/255.0 alpha:1.0];
label.shadowOffset = CGSizeMake(0, 1);
label.shadowColor = [UIColor colorWithRed:0xe6/255.0 green:0xe7/255.0 blue:0xeb/255.0 alpha:1.0];
label.textAlignment = UITextAlignmentCenter;
label.font = [UIFont boldSystemFontOfSize:20.0];
UIView *labelContainer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, width, 24)];
[labelContainer addSubview:label];
self = [self initWithCustomView:labelContainer];
[label release];
[labelContainer release];
return self;
}
@end
//
// UIBarButtonItem+JFAdditions.h
//
// Created by Jacques Fortier on 1/10/11.
// Copyright 2011 Jacques Fortier.
// Released under the WTFPL (see LICENSE or http://sam.zoy.org/wtfpl/COPYING)
#import <UIKit/UIKit.h>
@interface UIBarButtonItem(JFAdditions)
- (id)initWithTitle:(NSString *)title width:(CGFloat)width;
@end
//
// UIBarButtonItem+JFAdditions.m
//
// Created by Jacques Fortier on 1/10/11.
// Copyright 2011 Jacques Fortier.
// Released under the WTFPL (see LICENSE or http://sam.zoy.org/wtfpl/COPYING)
#import "UIBarButtonItem+JFAdditions.h"
@implementation UIBarButtonItem(JFAdditions)
- (id)initWithTitle:(NSString *)title width:(CGFloat)width {
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, width, 23)];
label.text = title;
label.backgroundColor = [UIColor clearColor];
label.opaque = NO;
label.textColor = [UIColor colorWithRed:0x71/255.0 green:0x78/255.0 blue:0x80/255.0 alpha:1.0];
label.shadowOffset = CGSizeMake(0, 1);
label.shadowColor = [UIColor colorWithRed:0xe6/255.0 green:0xe7/255.0 blue:0xeb/255.0 alpha:1.0];
label.textAlignment = UITextAlignmentCenter;
label.font = [UIFont boldSystemFontOfSize:20.0];
UIView *labelContainer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, width, 24)];
[labelContainer addSubview:label];
self = [self initWithCustomView:labelContainer];
[label release];
[labelContainer release];
return self;
}
@end
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2004 Sam Hocevar <sam@hocevar.net>
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. You just DO WHAT THE FUCK YOU WANT TO.
@GerardRJ

In UIBarButtonItem+JFAdditions.m there should be a "label.text = title;" or the title doesn't get displayed.

@jacquesf
Owner
jacquesf commented Jan 7, 2012

D'oh, thanks! Fixed now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment