Created
April 19, 2012 19:35
-
-
Save shawnwall/2423583 to your computer and use it in GitHub Desktop.
UIView gradient inner border drawRect:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (void)drawRect:(CGRect)rect | |
{ | |
CGContextRef ctx = UIGraphicsGetCurrentContext(); | |
UIBezierPath *path = [UIBezierPath bezierPathWithRect:rect]; | |
//To create a 1.0f borderWidth | |
CGRect innerRect = rect; | |
innerRect.origin.x += 1.0f; | |
innerRect.origin.y += 1.0f; | |
innerRect.size.width -= 2.0f; | |
innerRect.size.height -= 2.0f; | |
UIBezierPath *innerPath = [UIBezierPath bezierPathWithRect:innerRect]; | |
[path appendPath:innerPath]; | |
path.usesEvenOddFillRule = YES; | |
[path addClip]; | |
CGGradientRef gradient; | |
CGColorSpaceRef colorspace; | |
size_t num_locations = 2; | |
CGFloat locations[2] = { 0.0, 1.0 }; | |
CGFloat components[8] = { 1.0, 0.0, 0.0, 1.0, // Start color | |
0.0, 1.0, 0.0, 1.0 }; // End color | |
colorspace = CGColorSpaceCreateDeviceRGB(); | |
gradient = CGGradientCreateWithColorComponents (colorspace, components, locations, num_locations); | |
CGContextAddPath(ctx, path.CGPath); | |
CGContextClip(ctx); | |
CGContextDrawLinearGradient (ctx, gradient, CGPointMake(-1.0f, 0.0f), CGPointMake(0.0f, 480.0f), 0); | |
CGGradientRelease(gradient); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment