Skip to content

Commit

Permalink
IPDashedBorderedView and UIAppearance support.
Browse files Browse the repository at this point in the history
added dashed bordered view
added UIAppearance support.
added example from a .xib.
modernized enum.
updated podspec.
updated README.
reorganized file structure.
  • Loading branch information
Colin Brash committed Jun 27, 2014
1 parent 3943d3b commit b46ae30
Show file tree
Hide file tree
Showing 9 changed files with 238 additions and 20 deletions.
40 changes: 40 additions & 0 deletions Code/IPDashedBorderedView.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//
// IPDashedBorderedView.h
// IPDashedLine
//
// Created by Colin Brash on 6/27/14.
// Copyright (c) 2014 Intrepid Pursuits. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface IPDashedBorderedView : UIView <UIAppearanceContainer>

/**
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
63 changes: 63 additions & 0 deletions Code/IPDashedBorderedView.m
Original file line number Diff line number Diff line change
@@ -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
15 changes: 7 additions & 8 deletions IPDashedLineView.h → Code/IPDashedLineView.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,33 +9,32 @@

#import <UIKit/UIKit.h>


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 <UIAppearanceContainer>

/**
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
Expand All @@ -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
File renamed without changes.
33 changes: 26 additions & 7 deletions Example/IPDashedLine.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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 */; };
Expand All @@ -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 = "<group>"; };
4900EAD3195DF21E00D93D1A /* IPDashedBorderedView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = IPDashedBorderedView.m; sourceTree = "<group>"; };
4900EAD5195DF35F00D93D1A /* IPViewController.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = IPViewController.xib; sourceTree = "<group>"; };
4983FDE71958AA4A00247750 /* Images.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Images.xcassets; sourceTree = "<group>"; };
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; };
Expand All @@ -30,8 +35,8 @@
49F32A37193913B6007B882F /* IPAppDelegate.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = IPAppDelegate.m; sourceTree = "<group>"; };
49F32A3F193913B6007B882F /* IPViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = IPViewController.h; sourceTree = "<group>"; };
49F32A40193913B6007B882F /* IPViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = IPViewController.m; sourceTree = "<group>"; };
49F32A5F193913CE007B882F /* IPDashedLineView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = IPDashedLineView.h; path = ../IPDashedLineView.h; sourceTree = "<group>"; };
49F32A60193913CE007B882F /* IPDashedLineView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = IPDashedLineView.m; path = ../IPDashedLineView.m; sourceTree = "<group>"; };
49F32A5F193913CE007B882F /* IPDashedLineView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = IPDashedLineView.h; sourceTree = "<group>"; };
49F32A60193913CE007B882F /* IPDashedLineView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = IPDashedLineView.m; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand All @@ -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 = "<group>";
};
49F32A1B193913B6007B882F = {
isa = PBXGroup;
children = (
49F32A2D193913B6007B882F /* IPDashedLine */,
4900EAD1195DF20C00D93D1A /* Code */,
49F32A2D193913B6007B882F /* Example */,
49F32A26193913B6007B882F /* Frameworks */,
49F32A25193913B6007B882F /* Products */,
);
Expand All @@ -75,19 +93,18 @@
name = Frameworks;
sourceTree = "<group>";
};
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 = "<group>";
};
49F32A2E193913B6007B882F /* Supporting Files */ = {
Expand Down Expand Up @@ -154,6 +171,7 @@
buildActionMask = 2147483647;
files = (
4983FDE81958AA4A00247750 /* Images.xcassets in Resources */,
4900EAD6195DF35F00D93D1A /* IPViewController.xib in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand All @@ -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 */,
Expand Down
34 changes: 34 additions & 0 deletions Example/IPViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#import "IPViewController.h"
#import "IPDashedLineView.h"
#import "IPDashedBorderedView.h"


@implementation IPViewController
Expand All @@ -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];

Expand Down Expand Up @@ -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
38 changes: 38 additions & 0 deletions Example/IPViewController.xib
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="5056" systemVersion="13D65" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES">
<dependencies>
<deployment defaultVersion="1552" identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="3733"/>
</dependencies>
<objects>
<placeholder placeholderIdentifier="IBFilesOwner" id="-1" userLabel="File's Owner" customClass="IPViewController">
<connections>
<outlet property="view" destination="iN0-l3-epB" id="4gJ-QB-M5N"/>
</connections>
</placeholder>
<placeholder placeholderIdentifier="IBFirstResponder" id="-2" customClass="UIResponder"/>
<view contentMode="scaleToFill" id="iN0-l3-epB">
<rect key="frame" x="0.0" y="0.0" width="320" height="568"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<view contentMode="scaleToFill" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="NtH-21-QCC" customClass="IPDashedBorderedView">
<rect key="frame" x="140" y="380" width="148" height="103"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<subviews>
<label opaque="NO" clipsSubviews="YES" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" fixedFrame="YES" text="This label is bordered!" textAlignment="center" lineBreakMode="wordWrap" numberOfLines="0" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" preferredMaxLayoutWidth="148" translatesAutoresizingMaskIntoConstraints="NO" id="jYn-MO-kms">
<rect key="frame" x="0.0" y="0.0" width="148" height="103"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<fontDescription key="fontDescription" type="system" pointSize="17"/>
<color key="textColor" cocoaTouchSystemColor="darkTextColor"/>
<nil key="highlightedColor"/>
</label>
</subviews>
<color key="backgroundColor" white="0.0" alpha="0.0" colorSpace="calibratedWhite"/>
</view>
</subviews>
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="calibratedWhite"/>
<simulatedStatusBarMetrics key="simulatedStatusBarMetrics"/>
<simulatedScreenMetrics key="simulatedDestinationMetrics" type="retina4"/>
</view>
</objects>
</document>
6 changes: 3 additions & 3 deletions IPDashedLineView.podspec
Original file line number Diff line number Diff line change
@@ -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 = {
Expand All @@ -27,9 +27,9 @@ Pod::Spec.new do |s|
s.author = { "Colin Brash" => "[email protected]" }
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
Loading

0 comments on commit b46ae30

Please sign in to comment.