Skip to content

Instantly share code, notes, and snippets.

@Shilo Shilo/SPTexture+Line.h
Last active Dec 20, 2015

Embed
What would you like to do?
A category for Sparrow that draws rounded lines via SPTexture and Core Graphics.
//
// 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
//
// 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
You can’t perform that action at this time.