SHOW:
|
|
- or go back to the newest paste.
| 1 | // | |
| 2 | // SampleButton.m | |
| 3 | // insetSample | |
| 4 | // | |
| 5 | // Copyright (c) 2012 Dirk-Willem van Gulik. All rights reserved. | |
| 6 | // | |
| 7 | // Licensed under the ASL or BSD license. | |
| 8 | ||
| 9 | #import "SampleButton.h" | |
| 10 | ||
| 11 | @implementation SampleButton | |
| 12 | ||
| 13 | - (id)initWithCoder:(NSCoder *)decoder {
| |
| 14 | self = [super initWithCoder:decoder]; | |
| 15 | if (self) {
| |
| 16 | [self completeInit]; | |
| 17 | } | |
| 18 | return self; | |
| 19 | } | |
| 20 | ||
| 21 | -(void)setType:(int)aType { type = aType; [self completeInit]; }
| |
| 22 | -(int)type { return type; }
| |
| 23 | ||
| 24 | -(void)completeInit {
| |
| 25 | UIImage * base = [UIImage imageNamed:@"button.png"]; | |
| 26 | UIEdgeInsets edgeInsets = UIEdgeInsetsMake(20,20,20,20); | |
| 27 | ||
| 28 | CGContextRef ctx; | |
| 29 | UIImage * img; | |
| 30 | ||
| 31 | switch (type) {
| |
| 32 | case 1: // with insets, drawn straight | |
| 33 | base = [base resizableImageWithCapInsets:edgeInsets]; | |
| 34 | case 0: // without insets, drawn straight | |
| 35 | img = base; | |
| 36 | break; | |
| 37 | case 3: // with insets; dranw through an image context | |
| 38 | base = [base resizableImageWithCapInsets:edgeInsets]; | |
| 39 | case 2: // without insets; dranw through an image context | |
| 40 | UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, 0); | |
| 41 | ctx = UIGraphicsGetCurrentContext(); | |
| 42 | CGContextDrawImage(ctx, CGRectMake(0,0,self.bounds.size.width, | |
| 43 | self.bounds.size.height), [base CGImage]); | |
| 44 | ||
| 45 | img = UIGraphicsGetImageFromCurrentImageContext(); | |
| 46 | UIGraphicsEndImageContext(); | |
| 47 | break; | |
| 48 | default: | |
| 49 | break; | |
| 50 | } | |
| 51 | ||
| 52 | [self setImage:img forState:UIControlStateNormal]; | |
| 53 | ||
| 54 | // Esnure that the image is (also) scaled 'up' and not just down in | |
| 55 | // a non aspect preserving manner. | |
| 56 | // | |
| 57 | [[self imageView] setContentMode: UIViewContentModeScaleToFill]; | |
| 58 | self.contentHorizontalAlignment = UIControlContentHorizontalAlignmentFill; | |
| 59 | self.contentVerticalAlignment = UIControlContentVerticalAlignmentFill; | |
| 60 | ||
| 61 | } | |
| 62 | ||
| 63 | @end |