Skip to content

Instantly share code, notes, and snippets.

@cypres
Created February 8, 2012 10:57
Show Gist options
  • Select an option

  • Save cypres/1768185 to your computer and use it in GitHub Desktop.

Select an option

Save cypres/1768185 to your computer and use it in GitHub Desktop.
ios, manual end caps
#import "BackButton.h"
@implementation BackButton
-(void) awakeFromNib {
[self setBackgroundImage:nil forState:UIControlStateNormal];
}
-(void) drawRect:(CGRect)rect {
CGContextRef context = UIGraphicsGetCurrentContext();
CGRect bounds = [self bounds];
int leftCapWidth = 18;
int rightCapWidth = 5;
int height = 32;
// Assume retina image so *2 coordinates
CGImageRef background = [[UIImage imageNamed:@"backbutton.png"] CGImage];
CGImageRef leftCap = CGImageCreateWithImageInRect(background, CGRectMake(0.0, 0.0, leftCapWidth*2, height*2));
CGImageRef middleSection = CGImageCreateWithImageInRect(background, CGRectMake(leftCapWidth*2, 0.0, leftCapWidth, height*2));
CGImageRef rightCap = CGImageCreateWithImageInRect(background, CGRectMake(CGImageGetWidth(background)-rightCapWidth*2, 0.0, rightCapWidth*2, height*2));
CGContextDrawImage(context, CGRectMake(0.0, 0, leftCapWidth, CGRectGetHeight(bounds)), leftCap);
CGContextDrawImage(context, CGRectMake(CGRectGetWidth(bounds)-rightCapWidth, 0, rightCapWidth, CGRectGetHeight(bounds)), rightCap);
CGContextDrawImage(context, CGRectMake(leftCapWidth, 0, CGRectGetWidth(bounds)-rightCapWidth-leftCapWidth, CGRectGetHeight(bounds)), middleSection);
CGImageRelease(leftCap);
CGImageRelease(middleSection);
CGImageRelease(rightCap);
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment