Last active
December 20, 2015 14:19
-
-
Save Shilo/6145901 to your computer and use it in GitHub Desktop.
A category for Sparrow that draws rounded lines via SPTexture and Core Graphics.
This file contains hidden or 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
// | |
// SPTexture+Line.h | |
// Sparrow | |
// | |
// Created by Shilo White on 8/3/13. | |
// | |
#import "SPTexture.h" | |
#import "SPImage.h" | |
@interface SPTexture (Line) | |
- (id)initWithLineLength:(float)length width:(float)width rounded:(BOOL)rounded; | |
- (id)initWithLineLength:(float)length width:(float)width rounded:(BOOL)rounded padding:(float)padding; | |
+ (id)textureWithLineLength:(float)length width:(float)width rounded:(BOOL)rounded; | |
+ (id)textureWithLineLength:(float)length width:(float)width rounded:(BOOL)rounded padding:(float)padding; | |
@end | |
@interface SPImage (Line) | |
- (id)initWithLineLength:(float)length width:(float)width rounded:(BOOL)rounded; | |
- (id)initWithLineLength:(float)length width:(float)width rounded:(BOOL)rounded padding:(float)padding; | |
+ (id)imageWithLineLength:(float)length width:(float)width rounded:(BOOL)rounded; | |
+ (id)imageWithLineLength:(float)length width:(float)width rounded:(BOOL)rounded padding:(float)padding; | |
@end |
This file contains hidden or 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
// | |
// SPTexture+Line.m | |
// Sparrow | |
// | |
// Created by Shilo White on 8/3/13. | |
// | |
#import "SPTexture+Line.h" | |
@implementation SPTexture (Line) | |
- (id)initWithLineLength:(float)length width:(float)width rounded:(BOOL)rounded | |
{ | |
return [self initWithLineLength:length width:width rounded:rounded padding:0.0f]; | |
} | |
- (id)initWithLineLength:(float)length width:(float)width rounded:(BOOL)rounded padding:(float)padding | |
{ | |
return [self initWithWidth:width+(padding*2) height:length+(padding*2) draw:^(CGContextRef context) | |
{ | |
float halfWidth = width/2.0f; | |
float capWidth = rounded ? halfWidth : 0; | |
CGContextSetLineWidth(context, width); | |
if (rounded) CGContextSetLineCap(context, kCGLineCapRound); | |
CGContextMoveToPoint(context, halfWidth+padding, capWidth+padding); | |
CGContextAddLineToPoint(context, halfWidth+padding, length-capWidth+padding); | |
CGContextSetStrokeColorWithColor(context, [UIColor whiteColor].CGColor); | |
CGContextStrokePath(context); | |
}]; | |
} | |
+ (id)textureWithLineLength:(float)length width:(float)width rounded:(BOOL)rounded | |
{ | |
return [[self alloc] initWithLineLength:length width:width rounded:rounded]; | |
} | |
+ (id)textureWithLineLength:(float)length width:(float)width rounded:(BOOL)rounded padding:(float)padding | |
{ | |
return [[self alloc] initWithLineLength:length width:width rounded:rounded padding:padding]; | |
} | |
@end | |
@implementation SPImage (Line) | |
- (id)initWithLineLength:(float)length width:(float)width rounded:(BOOL)rounded | |
{ | |
return [self initWithLineLength:length width:width rounded:rounded padding:0.0f]; | |
} | |
- (id)initWithLineLength:(float)length width:(float)width rounded:(BOOL)rounded padding:(float)padding | |
{ | |
return [self initWithTexture:[SPTexture textureWithLineLength:length width:width rounded:rounded padding:padding]]; | |
} | |
+ (id)imageWithLineLength:(float)length width:(float)width rounded:(BOOL)rounded | |
{ | |
return [[self alloc] initWithLineLength:length width:width rounded:rounded]; | |
} | |
+ (id)imageWithLineLength:(float)length width:(float)width rounded:(BOOL)rounded padding:(float)padding | |
{ | |
return [[self alloc] initWithLineLength:length width:width rounded:rounded padding:padding]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment