|
#import "DropdownButton.h" |
|
|
|
@implementation DropdownButton |
|
|
|
@synthesize gradientLayer; |
|
@synthesize dropdownLayer; |
|
@synthesize triangleLayer; |
|
@synthesize label; |
|
|
|
- (id)initWithFrame:(CGRect)frame |
|
{ |
|
self = [super initWithFrame:frame]; |
|
if (self) { |
|
[self initializeView]; |
|
} |
|
return self; |
|
} |
|
|
|
- (id)init |
|
{ |
|
self = [super init]; |
|
if (self) { |
|
[self initializeView]; |
|
} |
|
return self; |
|
} |
|
|
|
- (id)initWithCoder:(NSCoder *)aDecoder |
|
{ |
|
self = [super initWithCoder:aDecoder]; |
|
if (self) { |
|
[self initializeView]; |
|
} |
|
return self; |
|
} |
|
|
|
- (void)dealloc |
|
{ |
|
[label release], label = nil; |
|
[super dealloc]; |
|
} |
|
|
|
- (void)initializeView |
|
{ |
|
[[self layer] setBorderColor:[[UIColor darkGrayColor] CGColor]]; |
|
[[self layer] setBorderWidth:1.0f]; |
|
[[self layer] setCornerRadius:8.0f]; |
|
[[self layer] setMasksToBounds:YES]; |
|
|
|
if (!gradientLayer) { |
|
gradientLayer = [CAGradientLayer layer]; |
|
[gradientLayer setBounds:[self bounds]]; |
|
[gradientLayer setPosition:CGPointMake([self bounds].size.width/2.0f, [self bounds].size.height/2.0f)]; |
|
[gradientLayer setColors:[NSArray arrayWithObjects:(id)[[UIColor colorWithRed:253.0f/255.0f green:253.0f/255.0f blue:253.0f/255.0f alpha:1.0f] CGColor], (id)[[UIColor colorWithRed:191.0f/255.0f green:191.0f/255.0f blue:191.0f/255.0f alpha:1.0f] CGColor], nil]]; |
|
[[self layer] addSublayer:gradientLayer]; |
|
} |
|
|
|
if (!dropdownLayer) { |
|
dropdownLayer = [CAGradientLayer layer]; |
|
[dropdownLayer setBorderColor:[[UIColor darkGrayColor] CGColor]]; |
|
[dropdownLayer setBorderWidth:1.0f]; |
|
[dropdownLayer setBounds:CGRectMake(0.0f, 0.0f, 31.0f, [self bounds].size.height+2)]; |
|
CGPoint point = CGPointMake([self bounds].size.width - 15.0f, [self bounds].size.height/2.0f); |
|
[dropdownLayer setPosition:point]; |
|
[dropdownLayer setColors:[NSArray arrayWithObjects:(id)[[UIColor colorWithRed:141.0f/255.0f green:141.0f/255.0f blue:141.0f/255.0f alpha:1.0f] CGColor], (id)[[UIColor colorWithRed:100.0f/255.0f green:100.0f/255.0f blue:100.0f/255.0f alpha:1.0f] CGColor], nil]]; |
|
[[self layer] addSublayer:dropdownLayer]; |
|
|
|
} |
|
|
|
if (!triangleLayer) { |
|
triangleLayer = [CAShapeLayer layer]; |
|
CGMutablePathRef path = CGPathCreateMutable(); |
|
CGPathMoveToPoint(path, nil, 0.0f, 0.0f); |
|
CGPathAddLineToPoint(path, nil, 10.0f, 0.0f); |
|
CGPathAddLineToPoint(path, nil, 5.0f, 10.0f); |
|
CGPathAddLineToPoint(path, nil, 0.0f, 0.0f); |
|
[triangleLayer setPath:path]; |
|
CGPathRelease(path); |
|
[triangleLayer setFillColor:[[UIColor whiteColor] CGColor]]; |
|
CGRect boundingBox = CGPathGetBoundingBox(path); |
|
[triangleLayer setBounds:boundingBox]; |
|
[triangleLayer setPosition:CGPointMake([dropdownLayer bounds].size.width/2.0f, [dropdownLayer bounds].size.height/2.0f)]; |
|
[dropdownLayer addSublayer:triangleLayer]; |
|
} |
|
|
|
if (!label) { |
|
label = [[UILabel alloc] init]; |
|
CGRect startRect = [self bounds]; |
|
startRect.origin.x = 10.0f; |
|
[label setUserInteractionEnabled:NO]; |
|
[label setFrame:startRect]; |
|
[label setBackgroundColor:[UIColor clearColor]]; |
|
[self addSubview:label]; |
|
} |
|
} |
|
|
|
- (void)setText:(NSString*)txt |
|
{ |
|
[[self label] setText:txt]; |
|
} |
|
|
|
- (NSString*)text |
|
{ |
|
return [[self label] text]; |
|
} |
|
|
|
|
|
@end |
Here is an image of what the button looks like when displayed.