Skip to content

Instantly share code, notes, and snippets.

@henrik
Created March 22, 2010 06:47
Show Gist options
  • Save henrik/339850 to your computer and use it in GitHub Desktop.
Save henrik/339850 to your computer and use it in GitHub Desktop.
Rounded rectangle with gradient background on the iPhone, NSTokenField/NSTokenFieldCell style. Kind of like in Mail.app.
// AKToken
// By Henrik Nyh <http://henrik.nyh.se> 2010-03-22 under the MIT license.
#import <UIKit/UIKit.h>
@interface AKToken : UIView {
}
- (id)initWithFrame:(CGRect)frame andColor:(UIColor *)color andText:(NSString *)text;
@end
#import "AKToken.h"
// Make sure you include this framework.
#import <QuartzCore/QuartzCore.h>
// Also include this: http://github.com/ars/uicolor-utilities/
#import "UIColor-Expanded.h"
@implementation AKToken
- (id)initWithFrame:(CGRect)frame andColor:(UIColor *)color andText:(NSString *)text {
if ((self = [super initWithFrame:frame])) {
UIColor *darkerColor = color;
UIColor *lighterColor = [darkerColor colorByLighteningTo:0.95];
UIColor *borderColor = [darkerColor colorByDarkeningTo:0.9];
CAGradientLayer *gradient = [[CAGradientLayer alloc] init];
gradient.frame = self.bounds;
gradient.colors = [NSArray arrayWithObjects:(id)lighterColor.CGColor, (id)darkerColor.CGColor, nil];
[self.layer insertSublayer:gradient atIndex:0];
[gradient release];
self.layer.cornerRadius = frame.size.height/2.0;
self.layer.borderColor = borderColor.CGColor;
self.layer.borderWidth = 1.0;
self.layer.masksToBounds = YES;
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(7.0, 0, self.bounds.size.width-14.0, self.bounds.size.height)];
[self addSubview:label];
[label release];
label.text = text;
label.font = [UIFont systemFontOfSize:14.0];
label.minimumFontSize = 11.0;
label.adjustsFontSizeToFitWidth = YES;
label.opaque = NO;
label.backgroundColor = [UIColor clearColor];
label.shadowColor = [UIColor colorWithWhite:1.0 alpha:0.4];
label.shadowOffset = CGSizeMake(1, 1);
label.textAlignment = UITextAlignmentCenter;
}
return self;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment