Skip to content

Instantly share code, notes, and snippets.

@timonus
Created September 10, 2015 02:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timonus/9f0d646f4b1b4b1dbe2f to your computer and use it in GitHub Desktop.
Save timonus/9f0d646f4b1b4b1dbe2f to your computer and use it in GitHub Desktop.
tijo Logo Objective-C Source
//
// TJLogoView.h
// Logo
//
// Created by Tim Johnsen on 9/7/15.
// Copyright (c) 2015 tijo. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface TJLogoView : UIView
@property (nonatomic, assign) CGFloat gridWidth;
@property (nonatomic, assign) CGFloat gridHeight;
@property (nonatomic, strong) UIColor *strokeColor;
@property (nonatomic, assign) CGFloat strokeWidth;
+ (void)saveToPathWithSize:(CGSize)size backgroundColor:(UIColor *)backgroundColor strokeColor:(UIColor *)strokeColor path:(NSString *)path;
@end
//
// TJLogoView.m
// Logo
//
// Created by Tim Johnsen on 9/7/15.
// Copyright (c) 2015 tijo. All rights reserved.
//
#import "TJLogoView.h"
@implementation TJLogoView
- (instancetype)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame]) {
[self updateMetricsFromFrame:frame];
self.strokeColor = [UIColor blackColor];
self.backgroundColor = [UIColor clearColor];
}
return self;
}
- (void)setFrame:(CGRect)frame
{
[super setFrame:frame];
[self updateMetricsFromFrame:frame];
}
- (void)updateMetricsFromFrame:(CGRect)frame
{
// Adjust!
const CGFloat minDimension = MIN(frame.size.width, frame.size.height);
self.gridWidth = floor(minDimension / 5.0);
self.gridHeight = floor(self.gridWidth * (48.0 / 52.0));
self.strokeWidth = floor(self.gridWidth * (20.0 / 58.0));
}
- (void)drawRect:(CGRect)rect
{
[super drawRect:rect];
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetStrokeColorWithColor(context, [self.strokeColor CGColor]);
// T
UIBezierPath *tPath = [[UIBezierPath alloc] init];
tPath.lineWidth = self.strokeWidth;
tPath.lineCapStyle = kCGLineCapSquare;
[tPath moveToPoint:CGPointMake(1 * self.gridWidth, 1 * self.gridHeight)];
[tPath addLineToPoint:CGPointMake(1 * self.gridWidth, 3 * self.gridHeight)];
[tPath moveToPoint:CGPointMake(0.5 * self.gridWidth, 1 * self.gridHeight)];
[tPath addLineToPoint:CGPointMake(1.5 * self.gridWidth, 1 * self.gridHeight)];
[tPath stroke];
// I
UIBezierPath *iPath = [[UIBezierPath alloc] init];
iPath.lineWidth = self.strokeWidth;
iPath.lineCapStyle = kCGLineCapSquare;
[iPath moveToPoint:CGPointMake(2 * self.gridWidth, 1 * self.gridHeight)];
[iPath addLineToPoint:CGPointMake(2 * self.gridWidth, 3 * self.gridHeight)];
[iPath stroke];
// I Dot
UIBezierPath *iDotPath = [[UIBezierPath alloc] init];
iDotPath.lineWidth = self.strokeWidth;
iDotPath.lineCapStyle = kCGLineCapSquare;
[iDotPath moveToPoint:CGPointMake(1.5 * self.gridWidth, 0.5 * self.gridHeight)];
[iDotPath addLineToPoint:CGPointMake(1.5 * self.gridWidth, 0.5 * self.gridHeight - 1.0)];
[iDotPath stroke];
// J
static const CGFloat kCircleFraction = 0.8;
UIBezierPath *jPath = [[UIBezierPath alloc] init];
jPath.lineWidth = self.strokeWidth;
jPath.lineCapStyle = kCGLineCapSquare;
[jPath moveToPoint:CGPointMake(3 * self.gridWidth, 1 * self.gridHeight)];
[jPath addLineToPoint:CGPointMake(3 * self.gridWidth, 3 * self.gridHeight)];
[jPath addArcWithCenter:CGPointMake(2 * self.gridWidth, 3 * self.gridHeight) radius:self.gridWidth startAngle:0.0 endAngle:M_PI * kCircleFraction clockwise:YES];
[jPath stroke];
// J Dot
UIBezierPath *jDotPath = [[UIBezierPath alloc] init];
jDotPath.lineWidth = self.strokeWidth;
jDotPath.lineCapStyle = kCGLineCapSquare;
[jDotPath moveToPoint:CGPointMake(2.5 * self.gridWidth, 0.5 * self.gridHeight)];
[jDotPath addLineToPoint:CGPointMake(2.5 * self.gridWidth, 0.5 * self.gridHeight - 1.0)];
[jDotPath stroke];
// O
UIBezierPath *oPath = [[UIBezierPath alloc] init];
oPath.lineWidth = self.strokeWidth;
oPath.lineCapStyle = kCGLineCapSquare;
[oPath moveToPoint:CGPointMake(3.5 * self.gridWidth, 1.5 * self.gridHeight)];
[oPath addLineToPoint:CGPointMake(3.5 * self.gridWidth, 2.5 * self.gridHeight)];
[oPath addArcWithCenter:CGPointMake(4 * self.gridWidth, 2.5 * self.gridHeight) radius:self.gridWidth / 2.0 startAngle:M_PI endAngle:0.0 clockwise:NO];
[oPath addLineToPoint:CGPointMake(4.5 * self.gridWidth, 1.5 * self.gridHeight)];
[oPath addArcWithCenter:CGPointMake(4 * self.gridWidth, 1.5 * self.gridHeight) radius:self.gridWidth / 2.0 startAngle:0.0 endAngle:M_PI clockwise:NO];
[oPath closePath];
[oPath stroke];
}
- (void)drawGridWithColor:(UIColor *)color width:(CGFloat)width
{
static const NSInteger horizontalGridLineCount = 10;
static const NSInteger verticalGridLineCount = 8;
CGContextSetStrokeColorWithColor(UIGraphicsGetCurrentContext(), [color CGColor]);
for (NSInteger x = 1; x < horizontalGridLineCount; x++) {
UIBezierPath *verticalLinePath = [[UIBezierPath alloc] init];
verticalLinePath.lineWidth = width;
[verticalLinePath moveToPoint:CGPointMake(self.gridWidth * x / 2.0, 0)];
[verticalLinePath addLineToPoint:CGPointMake(self.gridWidth * x / 2.0, verticalGridLineCount * self.gridHeight / 2.0)];
[verticalLinePath stroke];
}
for (NSInteger y = 1; y < verticalGridLineCount; y++) {
UIBezierPath *horizontalLinePath = [[UIBezierPath alloc] init];
horizontalLinePath.lineWidth = width;
[horizontalLinePath moveToPoint:CGPointMake(0, self.gridHeight * y / 2.0)];
[horizontalLinePath addLineToPoint:CGPointMake(horizontalGridLineCount * self.gridWidth / 2.0, self.gridHeight * y / 2.0)];
[horizontalLinePath stroke];
}
}
+ (void)saveToPathWithSize:(CGSize)size backgroundColor:(UIColor *)backgroundColor strokeColor:(UIColor *)strokeColor path:(NSString *)path
{
TJLogoView *logoView = [[TJLogoView alloc] initWithFrame:CGRectMake(0.0, 0.0, size.width, size.height)];
logoView.strokeColor = strokeColor;
logoView.backgroundColor = backgroundColor;
UIGraphicsBeginImageContextWithOptions(logoView.bounds.size, NO, 2.0);
[logoView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[UIImagePNGRepresentation(image) writeToFile:path atomically:YES];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment