// // UIViewAdditions.m // Created by Devin Ross on 7/25/09. // /* tapku.com || http://github.com/devinross/tapkulibrary Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #import "UIView+TKCategory.h" @implementation UIView (TKCategory) CGPoint demoLGStart(CGRect bounds); CGPoint demoLGStart(CGRect bounds){ return CGPointMake(bounds.origin.x, bounds.origin.y + bounds.size.height * 0.25); }; CGPoint demoLGEnd(CGRect bounds); CGPoint demoLGEnd(CGRect bounds){ return CGPointMake(bounds.origin.x, bounds.origin.y + bounds.size.height * 0.75); } CGPoint demoRGCenter(CGRect bounds); CGPoint demoRGCenter(CGRect bounds){ return CGPointMake(CGRectGetMidX(bounds), CGRectGetMidY(bounds)); } CGFloat demoRGInnerRadius(CGRect bounds); CGFloat demoRGInnerRadius(CGRect bounds){ CGFloat r = bounds.size.width < bounds.size.height ? bounds.size.width : bounds.size.height; return r * 0.125; } + (void) drawGradientInRect:(CGRect)rect withColors:(NSArray*)colors{ NSMutableArray *ar = [NSMutableArray array]; for(UIColor *c in colors){ [ar addObject:(id)c.CGColor]; } CGContextRef context = UIGraphicsGetCurrentContext(); CGContextSaveGState(context); CGColorSpaceRef colorSpace = CGColorGetColorSpace([[colors lastObject] CGColor]); CGGradientRef gradient = CGGradientCreateWithColors(colorSpace, (CFArrayRef)ar, NULL); CGContextClipToRect(context, rect); CGPoint start = CGPointMake(0.0, 0.0); CGPoint end = CGPointMake(0.0, rect.size.height); CGContextDrawLinearGradient(context, gradient, start, end, kCGGradientDrawsBeforeStartLocation | kCGGradientDrawsAfterEndLocation); CGGradientRelease(gradient); CGContextRestoreGState(context); } + (void) drawLinearGradientInRect:(CGRect)rect colors:(CGFloat[])colours{ CGContextRef context = UIGraphicsGetCurrentContext(); CGContextSaveGState(context); CGColorSpaceRef rgb = CGColorSpaceCreateDeviceRGB(); CGGradientRef gradient = CGGradientCreateWithColorComponents(rgb, colours, NULL, 2); CGColorSpaceRelease(rgb); CGPoint start, end; start = demoLGStart(rect); end = demoLGEnd(rect); CGContextClipToRect(context, rect); CGContextDrawLinearGradient(context, gradient, start, end, kCGGradientDrawsBeforeStartLocation | kCGGradientDrawsAfterEndLocation); CGGradientRelease(gradient); CGContextRestoreGState(context); } + (void) drawRoundRectangleInRect:(CGRect)rect withRadius:(CGFloat)radius{ CGContextRef context = UIGraphicsGetCurrentContext(); CGRect rrect = CGRectMake(rect.origin.x, rect.origin.y, rect.size.width, rect.size.height ); CGFloat minx = CGRectGetMinX(rrect), midx = CGRectGetMidX(rrect), maxx = CGRectGetMaxX(rrect); CGFloat miny = CGRectGetMinY(rrect), midy = CGRectGetMidY(rrect), maxy = CGRectGetMaxY(rrect); CGContextMoveToPoint(context, minx, midy); CGContextAddArcToPoint(context, minx, miny, midx, miny, radius); CGContextAddArcToPoint(context, maxx, miny, maxx, midy, radius); CGContextAddArcToPoint(context, maxx, maxy, midx, maxy, radius); CGContextAddArcToPoint(context, minx, maxy, minx, midy, radius); CGContextClosePath(context); CGContextDrawPath(context, kCGPathFill); } + (void) drawLineInRect:(CGRect)rect colors:(CGFloat[])colors { [UIView drawLineInRect:rect colors:colors width:1 cap:kCGLineCapButt]; } + (void) drawLineInRect:(CGRect)rect red:(CGFloat)red green:(CGFloat)green blue:(CGFloat)blue alpha:(CGFloat)alpha{ CGFloat colors[4]; colors[0] = red; colors[1] = green; colors[2] = blue; colors[3] = alpha; [UIView drawLineInRect:rect colors:colors]; } + (void) drawLineInRect:(CGRect)rect colors:(CGFloat[])colors width:(CGFloat)lineWidth cap:(CGLineCap)cap{ CGContextRef context = UIGraphicsGetCurrentContext(); CGContextSaveGState(context); CGContextSetRGBStrokeColor(context, colors[0], colors[1], colors[2], colors[3]); CGContextSetLineCap(context,cap); CGContextSetLineWidth(context, lineWidth); CGContextMoveToPoint(context, rect.origin.x, rect.origin.y); CGContextAddLineToPoint(context,rect.origin.x+rect.size.width, rect.origin.y+rect.size.height); CGContextStrokePath(context); CGContextRestoreGState(context); } @end