From b46ae30bdf6189d6dff9116e92caa46e78f64949 Mon Sep 17 00:00:00 2001 From: Colin Brash Date: Fri, 27 Jun 2014 14:44:39 -0400 Subject: [PATCH] IPDashedBorderedView and UIAppearance support. added dashed bordered view added UIAppearance support. added example from a .xib. modernized enum. updated podspec. updated README. reorganized file structure. --- Code/IPDashedBorderedView.h | 40 ++++++++++++ Code/IPDashedBorderedView.m | 63 +++++++++++++++++++ IPDashedLineView.h => Code/IPDashedLineView.h | 15 +++-- IPDashedLineView.m => Code/IPDashedLineView.m | 0 .../IPDashedLine.xcodeproj/project.pbxproj | 33 +++++++--- Example/IPViewController.m | 34 ++++++++++ Example/IPViewController.xib | 38 +++++++++++ IPDashedLineView.podspec | 6 +- README.md | 29 ++++++++- 9 files changed, 238 insertions(+), 20 deletions(-) create mode 100644 Code/IPDashedBorderedView.h create mode 100644 Code/IPDashedBorderedView.m rename IPDashedLineView.h => Code/IPDashedLineView.h (78%) rename IPDashedLineView.m => Code/IPDashedLineView.m (100%) create mode 100644 Example/IPViewController.xib diff --git a/Code/IPDashedBorderedView.h b/Code/IPDashedBorderedView.h new file mode 100644 index 0000000..fe5070d --- /dev/null +++ b/Code/IPDashedBorderedView.h @@ -0,0 +1,40 @@ +// +// IPDashedBorderedView.h +// IPDashedLine +// +// Created by Colin Brash on 6/27/14. +// Copyright (c) 2014 Intrepid Pursuits. All rights reserved. +// + +#import + +@interface IPDashedBorderedView : UIView + +/** + How many pts into the drawing we start, from the top left. + Default is 0. + */ +@property (assign, nonatomic) CGFloat phase UI_APPEARANCE_SELECTOR; + +/** + Passing an array with the values [2,3] sets a dash pattern that alternates between a + 2 pt lineColor painted segment and a 3 pt backgroundColor unpainted + segment. Passing the values [1,3,4,2] sets the pattern to a 1-unit painted segment, + a 3-unit unpainted segment, a 4-unit painted segment, and a 2-unit unpainted segment. + Default is @[@2, @2]. + */ +@property (strong, nonatomic) NSArray *lengthPattern UI_APPEARANCE_SELECTOR; + +/** + Color of the dashes. Use backgroundColor for the non-dash color. + Default is black. + */ +@property (strong, nonatomic) UIColor *lineColor UI_APPEARANCE_SELECTOR; + +/** + Width of the dashed lines. + Default is 1. + */ +@property (assign, nonatomic) CGFloat borderWidth UI_APPEARANCE_SELECTOR; + +@end diff --git a/Code/IPDashedBorderedView.m b/Code/IPDashedBorderedView.m new file mode 100644 index 0000000..00510c0 --- /dev/null +++ b/Code/IPDashedBorderedView.m @@ -0,0 +1,63 @@ +// +// IPDashedBorderedView.m +// IPDashedLine +// +// Created by Colin Brash on 6/27/14. +// Copyright (c) 2014 Intrepid Pursuits. All rights reserved. +// + +#import "IPDashedBorderedView.h" + +@implementation IPDashedBorderedView + +- (id)initWithCoder:(NSCoder *)aDecoder { + self = [super initWithCoder:aDecoder]; + if (self) { + [self commonInit]; + } + return self; +} + +- (id)initWithFrame:(CGRect)frame { + self = [super initWithFrame:frame]; + if (self) { + [self commonInit]; + } + return self; +} + +- (void)commonInit { + self.backgroundColor = [UIColor clearColor]; + _phase = 0.0; + _lineColor = [UIColor blackColor]; + _lengthPattern = @[@2, @2]; + _borderWidth = 1.0; +} + +#pragma mark - + +- (void)drawRect:(CGRect)rect { + [super drawRect:rect]; + + CGContextRef context = UIGraphicsGetCurrentContext(); + + CGContextSetLineWidth(context, self.borderWidth * [UIScreen mainScreen].scale); + CGContextSetStrokeColorWithColor(context, self.lineColor.CGColor); + + int count = (int)[self.lengthPattern count]; + CGFloat cArrayLengthPattern[count]; + for (int i = 0; i < count; ++i) { + cArrayLengthPattern[i] = (CGFloat)[self.lengthPattern[i] floatValue]; + } + + CGContextSetLineDash(context, self.phase, cArrayLengthPattern, count); + CGContextMoveToPoint(context, rect.origin.x, rect.origin.y); + CGContextAddLineToPoint(context, CGRectGetMaxX(rect), rect.origin.y); + CGContextAddLineToPoint(context, CGRectGetMaxX(rect), CGRectGetMaxY(rect)); + CGContextAddLineToPoint(context, rect.origin.x, CGRectGetMaxY(rect)); + CGContextAddLineToPoint(context, rect.origin.x, rect.origin.y); + CGContextStrokePath(context); +} + + +@end diff --git a/IPDashedLineView.h b/Code/IPDashedLineView.h similarity index 78% rename from IPDashedLineView.h rename to Code/IPDashedLineView.h index 6515b0a..3ec4831 100644 --- a/IPDashedLineView.h +++ b/Code/IPDashedLineView.h @@ -9,33 +9,32 @@ #import - -typedef enum { +typedef NS_ENUM(NSInteger, IPDashedLineViewDirection) { IPDashedLineViewDirectionAutomatic = 0, IPDashedLineViewDirectionHorizontalFromLeft, IPDashedLineViewDirectionHorizontalFromRight, IPDashedLineViewDirectionVerticalFromTop, IPDashedLineViewDirectionVerticalFromBottom, -} IPDashedLineViewDirection; +}; /** UIView with a customizable dashed look, generally used for dashed lines. */ -@interface IPDashedLineView : UIView +@interface IPDashedLineView : UIView /** Forced direction of the line if this is not Automatic. Default is BBDashedLineViewDirectionAutomatic. This will auto select HorizontalFromLeft or VerticalFromTop depending on width vs height ratio. */ -@property (assign, nonatomic) IPDashedLineViewDirection direction; +@property (assign, nonatomic) IPDashedLineViewDirection direction UI_APPEARANCE_SELECTOR; /** How many pts into the drawing we start. Default is 0. */ -@property (assign, nonatomic) CGFloat phase; +@property (assign, nonatomic) CGFloat phase UI_APPEARANCE_SELECTOR; /** Passing an array with the values [2,3] sets a dash pattern that alternates between a @@ -44,12 +43,12 @@ typedef enum { a 3-unit unpainted segment, a 4-unit painted segment, and a 2-unit unpainted segment. Default is @[@2, @2]. */ -@property (strong, nonatomic) NSArray *lengthPattern; +@property (strong, nonatomic) NSArray *lengthPattern UI_APPEARANCE_SELECTOR; /** Color of the dashes. Use backgroundColor for the non-dash color. Default is black. */ -@property (strong, nonatomic) UIColor *lineColor; +@property (strong, nonatomic) UIColor *lineColor UI_APPEARANCE_SELECTOR; @end diff --git a/IPDashedLineView.m b/Code/IPDashedLineView.m similarity index 100% rename from IPDashedLineView.m rename to Code/IPDashedLineView.m diff --git a/Example/IPDashedLine.xcodeproj/project.pbxproj b/Example/IPDashedLine.xcodeproj/project.pbxproj index 80981af..070cf1a 100644 --- a/Example/IPDashedLine.xcodeproj/project.pbxproj +++ b/Example/IPDashedLine.xcodeproj/project.pbxproj @@ -7,6 +7,8 @@ objects = { /* Begin PBXBuildFile section */ + 4900EAD4195DF21E00D93D1A /* IPDashedBorderedView.m in Sources */ = {isa = PBXBuildFile; fileRef = 4900EAD3195DF21E00D93D1A /* IPDashedBorderedView.m */; }; + 4900EAD6195DF35F00D93D1A /* IPViewController.xib in Resources */ = {isa = PBXBuildFile; fileRef = 4900EAD5195DF35F00D93D1A /* IPViewController.xib */; }; 4983FDE81958AA4A00247750 /* Images.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 4983FDE71958AA4A00247750 /* Images.xcassets */; }; 49F32A28193913B6007B882F /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 49F32A27193913B6007B882F /* Foundation.framework */; }; 49F32A2A193913B6007B882F /* CoreGraphics.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 49F32A29193913B6007B882F /* CoreGraphics.framework */; }; @@ -18,6 +20,9 @@ /* End PBXBuildFile section */ /* Begin PBXFileReference section */ + 4900EAD2195DF21E00D93D1A /* IPDashedBorderedView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = IPDashedBorderedView.h; sourceTree = ""; }; + 4900EAD3195DF21E00D93D1A /* IPDashedBorderedView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = IPDashedBorderedView.m; sourceTree = ""; }; + 4900EAD5195DF35F00D93D1A /* IPViewController.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = IPViewController.xib; sourceTree = ""; }; 4983FDE71958AA4A00247750 /* Images.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Images.xcassets; sourceTree = ""; }; 49F32A24193913B6007B882F /* IPDashedLine.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = IPDashedLine.app; sourceTree = BUILT_PRODUCTS_DIR; }; 49F32A27193913B6007B882F /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = System/Library/Frameworks/Foundation.framework; sourceTree = SDKROOT; }; @@ -30,8 +35,8 @@ 49F32A37193913B6007B882F /* IPAppDelegate.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = IPAppDelegate.m; sourceTree = ""; }; 49F32A3F193913B6007B882F /* IPViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = IPViewController.h; sourceTree = ""; }; 49F32A40193913B6007B882F /* IPViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = IPViewController.m; sourceTree = ""; }; - 49F32A5F193913CE007B882F /* IPDashedLineView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = IPDashedLineView.h; path = ../IPDashedLineView.h; sourceTree = ""; }; - 49F32A60193913CE007B882F /* IPDashedLineView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = IPDashedLineView.m; path = ../IPDashedLineView.m; sourceTree = ""; }; + 49F32A5F193913CE007B882F /* IPDashedLineView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = IPDashedLineView.h; sourceTree = ""; }; + 49F32A60193913CE007B882F /* IPDashedLineView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = IPDashedLineView.m; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -48,10 +53,23 @@ /* End PBXFrameworksBuildPhase section */ /* Begin PBXGroup section */ + 4900EAD1195DF20C00D93D1A /* Code */ = { + isa = PBXGroup; + children = ( + 49F32A5F193913CE007B882F /* IPDashedLineView.h */, + 49F32A60193913CE007B882F /* IPDashedLineView.m */, + 4900EAD2195DF21E00D93D1A /* IPDashedBorderedView.h */, + 4900EAD3195DF21E00D93D1A /* IPDashedBorderedView.m */, + ); + name = Code; + path = ../Code; + sourceTree = ""; + }; 49F32A1B193913B6007B882F = { isa = PBXGroup; children = ( - 49F32A2D193913B6007B882F /* IPDashedLine */, + 4900EAD1195DF20C00D93D1A /* Code */, + 49F32A2D193913B6007B882F /* Example */, 49F32A26193913B6007B882F /* Frameworks */, 49F32A25193913B6007B882F /* Products */, ); @@ -75,19 +93,18 @@ name = Frameworks; sourceTree = ""; }; - 49F32A2D193913B6007B882F /* IPDashedLine */ = { + 49F32A2D193913B6007B882F /* Example */ = { isa = PBXGroup; children = ( - 49F32A5F193913CE007B882F /* IPDashedLineView.h */, - 49F32A60193913CE007B882F /* IPDashedLineView.m */, 49F32A36193913B6007B882F /* IPAppDelegate.h */, 49F32A37193913B6007B882F /* IPAppDelegate.m */, 49F32A3F193913B6007B882F /* IPViewController.h */, 49F32A40193913B6007B882F /* IPViewController.m */, + 4900EAD5195DF35F00D93D1A /* IPViewController.xib */, 4983FDE71958AA4A00247750 /* Images.xcassets */, 49F32A2E193913B6007B882F /* Supporting Files */, ); - name = IPDashedLine; + name = Example; sourceTree = ""; }; 49F32A2E193913B6007B882F /* Supporting Files */ = { @@ -154,6 +171,7 @@ buildActionMask = 2147483647; files = ( 4983FDE81958AA4A00247750 /* Images.xcassets in Resources */, + 4900EAD6195DF35F00D93D1A /* IPViewController.xib in Resources */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -164,6 +182,7 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( + 4900EAD4195DF21E00D93D1A /* IPDashedBorderedView.m in Sources */, 49F32A34193913B6007B882F /* main.m in Sources */, 49F32A61193913CE007B882F /* IPDashedLineView.m in Sources */, 49F32A38193913B6007B882F /* IPAppDelegate.m in Sources */, diff --git a/Example/IPViewController.m b/Example/IPViewController.m index 9ac259d..0cba9fb 100644 --- a/Example/IPViewController.m +++ b/Example/IPViewController.m @@ -8,6 +8,7 @@ #import "IPViewController.h" #import "IPDashedLineView.h" +#import "IPDashedBorderedView.h" @implementation IPViewController @@ -16,11 +17,20 @@ - (void)viewDidLoad { [super viewDidLoad]; + IPDashedLineView *appearance = [IPDashedLineView appearance]; + [appearance setLineColor:[UIColor redColor]]; + [appearance setLengthPattern:@[@4, @2]]; + self.view.backgroundColor = [UIColor whiteColor]; + // Using appearance + IPDashedLineView *dash0 = [[IPDashedLineView alloc] initWithFrame:CGRectMake(10, 30, 200, 1)]; + [self.view addSubview:dash0]; + // Forced Horizontal IPDashedLineView *dash1 = [[IPDashedLineView alloc] initWithFrame:CGRectMake(20, 60, 200, 1)]; dash1.direction = IPDashedLineViewDirectionHorizontalFromRight; + dash1.lineColor = [UIColor blackColor]; dash1.lengthPattern = @[@1, @1]; [self.view addSubview:dash1]; @@ -51,5 +61,29 @@ - (void)viewDidLoad dash5.lineColor = [UIColor purpleColor]; dash5.direction = IPDashedLineViewDirectionVerticalFromTop; [self.view addSubview:dash5]; + + // Bordered View + IPDashedBorderedView *borderedView1 = [[IPDashedBorderedView alloc] initWithFrame:CGRectMake(60, 140, 200, 100)]; + borderedView1.borderWidth = 5; + borderedView1.lineColor = [UIColor orangeColor]; + borderedView1.lengthPattern = @[@5, @5]; + borderedView1.backgroundColor = [UIColor blueColor]; + [self.view addSubview:borderedView1]; + + // Bordered View + IPDashedBorderedView *borderedView2 = [[IPDashedBorderedView alloc] initWithFrame:CGRectMake(120, 180, 40, 40)]; + borderedView2.borderWidth = 1; + borderedView2.lineColor = [UIColor whiteColor]; + borderedView2.lengthPattern = @[@1, @1]; + borderedView2.backgroundColor = [UIColor clearColor]; + [self.view addSubview:borderedView2]; + + // Bordered View + IPDashedBorderedView *borderedView3 = [[IPDashedBorderedView alloc] initWithFrame:CGRectMake(180, 180, 40, 40)]; + borderedView3.borderWidth = 1; + borderedView3.lineColor = [UIColor lightGrayColor]; + borderedView3.lengthPattern = @[@1, @3]; + borderedView3.backgroundColor = [UIColor clearColor]; + [self.view addSubview:borderedView3]; } @end diff --git a/Example/IPViewController.xib b/Example/IPViewController.xib new file mode 100644 index 0000000..3a1bbef --- /dev/null +++ b/Example/IPViewController.xib @@ -0,0 +1,38 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/IPDashedLineView.podspec b/IPDashedLineView.podspec index 8f9fbf6..e4b2f83 100644 --- a/IPDashedLineView.podspec +++ b/IPDashedLineView.podspec @@ -1,6 +1,6 @@ Pod::Spec.new do |s| s.name = "IPDashedLineView" - s.version = "1.0.0" + s.version = "1.1.0" s.summary = "Simple dashed lines." s.homepage = "https://github.com/IntrepidPursuits/IPDashedLineView" s.license = { @@ -27,9 +27,9 @@ Pod::Spec.new do |s| s.author = { "Colin Brash" => "colin3@intrepid.io" } s.source = { :git => "https://github.com/IntrepidPursuits/IPDashedLineView.git", - :tag => "1.0.0" + :tag => "1.1.0" } s.platform = :ios, '6.1' - s.source_files = 'IPDashedLineView.{h,m}' + s.source_files = 'Code/*.{h,m}' s.requires_arc = true end diff --git a/README.md b/README.md index b05025d..c085314 100644 --- a/README.md +++ b/README.md @@ -2,11 +2,14 @@ IPDashedLineView provides a simple interface for creating dashed lines. It can be instantiated in code with `-initWithFrame:` or in a nib. +![Screenshot](http://i.imgur.com/ROnEND9.png?1) + ## Example Usage // Forced Horizontal IPDashedLineView *dash1 = [[IPDashedLineView alloc] initWithFrame:CGRectMake(20, 60, 200, 1)]; dash1.direction = IPDashedLineViewDirectionHorizontalFromRight; + dash1.lineColor = [UIColor blackColor]; dash1.lengthPattern = @[@1, @1]; [self.view addSubview:dash1]; @@ -37,5 +40,27 @@ IPDashedLineView provides a simple interface for creating dashed lines. It can dash5.lineColor = [UIColor purpleColor]; dash5.direction = IPDashedLineViewDirectionVerticalFromTop; [self.view addSubview:dash5]; - -![Screenshot](http://i.imgur.com/I1m7KUk.png?1) + + // Bordered View + IPDashedBorderedView *borderedView1 = [[IPDashedBorderedView alloc] initWithFrame:CGRectMake(60, 140, 200, 100)]; + borderedView1.borderWidth = 5; + borderedView1.lineColor = [UIColor orangeColor]; + borderedView1.lengthPattern = @[@5, @5]; + borderedView1.backgroundColor = [UIColor blueColor]; + [self.view addSubview:borderedView1]; + + // Bordered View + IPDashedBorderedView *borderedView2 = [[IPDashedBorderedView alloc] initWithFrame:CGRectMake(120, 180, 40, 40)]; + borderedView2.borderWidth = 1; + borderedView2.lineColor = [UIColor whiteColor]; + borderedView2.lengthPattern = @[@1, @1]; + borderedView2.backgroundColor = [UIColor clearColor]; + [self.view addSubview:borderedView2]; + + // Bordered View + IPDashedBorderedView *borderedView3 = [[IPDashedBorderedView alloc] initWithFrame:CGRectMake(180, 180, 40, 40)]; + borderedView3.borderWidth = 1; + borderedView3.lineColor = [UIColor lightGrayColor]; + borderedView3.lengthPattern = @[@1, @3]; + borderedView3.backgroundColor = [UIColor clearColor]; + [self.view addSubview:borderedView3]; \ No newline at end of file