Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // SampleButton.m
- // insetSample
- //
- // Copyright (c) 2012 Dirk-Willem van Gulik. All rights reserved.
- //
- // Licensed under the ASL or BSD license.
- #import "SampleButton.h"
- @implementation SampleButton
- - (id)initWithCoder:(NSCoder *)decoder {
- self = [super initWithCoder:decoder];
- if (self) {
- [self completeInit];
- }
- return self;
- }
- -(void)setType:(int)aType { type = aType; [self completeInit]; }
- -(int)type { return type; }
- -(void)completeInit {
- UIImage * base = [UIImage imageNamed:@"button.png"];
- UIEdgeInsets edgeInsets = UIEdgeInsetsMake(20,20,20,20);
- CGContextRef ctx;
- UIImage * img;
- switch (type) {
- case 1: // with insets, drawn straight
- base = [base resizableImageWithCapInsets:edgeInsets];
- case 0: // without insets, drawn straight
- img = base;
- break;
- case 3: // with insets; dranw through an image context
- base = [base resizableImageWithCapInsets:edgeInsets];
- case 2: // without insets; dranw through an image context
- UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, 0);
- ctx = UIGraphicsGetCurrentContext();
- #if 1
- [base drawInRect:CGRectMake(0,0,self.bounds.size.width,
- self.bounds.size.height)];
- #else
- CGContextDrawImage(ctx, CGRectMake(0,0,self.bounds.size.width,
- self.bounds.size.height), [base CGImage]);
- #endif
- img = UIGraphicsGetImageFromCurrentImageContext();
- UIGraphicsEndImageContext();
- break;
- default:
- break;
- }
- [self setImage:img forState:UIControlStateNormal];
- // Esnure that the image is (also) scaled 'up' and not just down in
- // a non aspect preserving manner.
- //
- [[self imageView] setContentMode: UIViewContentModeScaleToFill];
- self.contentHorizontalAlignment = UIControlContentHorizontalAlignmentFill;
- self.contentVerticalAlignment = UIControlContentVerticalAlignmentFill;
- }
- @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement