Skip to content

Commit

Permalink
Fixed #3
Browse files Browse the repository at this point in the history
  • Loading branch information
jkmathew committed Aug 18, 2017
1 parent 50b45dc commit 68eb124
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 21 deletions.
19 changes: 19 additions & 0 deletions Example/JKSteppedProgressBar.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@
607FACCE1AFB9204008FA782 /* Resources */,
C607A2CD4D69FA5D866293A2 /* [CP] Embed Pods Frameworks */,
B5620B13ADE31FE3FEB49B48 /* [CP] Copy Pods Resources */,
4A4947DF1F46E52400BC624C /* ShellScript */,
);
buildRules = (
);
Expand Down Expand Up @@ -216,6 +217,7 @@
607FACCF1AFB9204008FA782 = {
CreatedOnToolsVersion = 6.3.1;
LastSwiftMigration = 0810;
ProvisioningStyle = Manual;
};
607FACE41AFB9204008FA782 = {
CreatedOnToolsVersion = 6.3.1;
Expand Down Expand Up @@ -264,6 +266,19 @@
/* End PBXResourcesBuildPhase section */

/* Begin PBXShellScriptBuildPhase section */
4A4947DF1F46E52400BC624C /* ShellScript */ = {
isa = PBXShellScriptBuildPhase;
buildActionMask = 2147483647;
files = (
);
inputPaths = (
);
outputPaths = (
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
shellScript = "";
};
A5B0DAFF32F00B676FB9EB45 /* [CP] Check Pods Manifest.lock */ = {
isa = PBXShellScriptBuildPhase;
buildActionMask = 2147483647;
Expand Down Expand Up @@ -497,11 +512,13 @@
buildSettings = {
ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES;
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
DEVELOPMENT_TEAM = "";
INFOPLIST_FILE = JKSteppedProgressBar/Info.plist;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
MODULE_NAME = ExampleApp;
PRODUCT_BUNDLE_IDENTIFIER = "org.cocoapods.demo.$(PRODUCT_NAME:rfc1034identifier)";
PRODUCT_NAME = "$(TARGET_NAME)";
PROVISIONING_PROFILE_SPECIFIER = "";
SWIFT_VERSION = 3.0;
};
name = Debug;
Expand All @@ -512,11 +529,13 @@
buildSettings = {
ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES;
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
DEVELOPMENT_TEAM = "";
INFOPLIST_FILE = JKSteppedProgressBar/Info.plist;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
MODULE_NAME = ExampleApp;
PRODUCT_BUNDLE_IDENTIFIER = "org.cocoapods.demo.$(PRODUCT_NAME:rfc1034identifier)";
PRODUCT_NAME = "$(TARGET_NAME)";
PROVISIONING_PROFILE_SPECIFIER = "";
SWIFT_VERSION = 3.0;
};
name = Release;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@
ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES"
debugServiceExtension = "internal"
allowLocationSimulation = "YES">
allowLocationSimulation = "YES"
language = "IDELaunchSchemeLanguageRightToLeftLayoutDirection">
<BuildableProductRunnable
runnableDebuggingMode = "0">
<BuildableReference
Expand Down
19 changes: 15 additions & 4 deletions Example/JKSteppedProgressBar/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="11201" systemVersion="15G1004" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="BYZ-38-t0r">
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="12120" systemVersion="16A323" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="BYZ-38-t0r">
<device id="retina4_7" orientation="portrait">
<adaptation id="fullscreen"/>
</device>
<dependencies>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="11161"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="12088"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<scenes>
Expand All @@ -19,13 +22,14 @@
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="Xx9-ek-0HE" customClass="SteppedProgressBar" customModule="JKSteppedProgressBar">
<rect key="frame" x="20" y="28" width="335" height="85"/>
<color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<constraints>
<constraint firstAttribute="height" constant="85" id="f2M-Vb-6Kw"/>
</constraints>
<userDefinedRuntimeAttributes>
<userDefinedRuntimeAttribute type="number" keyPath="lineWidth">
<real key="value" value="1"/>
<real key="value" value="6"/>
</userDefinedRuntimeAttribute>
<userDefinedRuntimeAttribute type="color" keyPath="inactiveTextColor">
<color key="value" red="0.66666666666666663" green="0.66666666666666663" blue="0.66666666666666663" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
Expand All @@ -42,9 +46,13 @@
<userDefinedRuntimeAttribute type="number" keyPath="currentTab">
<integer key="value" value="2"/>
</userDefinedRuntimeAttribute>
<userDefinedRuntimeAttribute type="number" keyPath="titleOffset">
<real key="value" value="2"/>
</userDefinedRuntimeAttribute>
</userDefinedRuntimeAttributes>
</view>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="eHz-z6-WGH">
<rect key="frame" x="219" y="184.5" width="30" height="30"/>
<constraints>
<constraint firstAttribute="height" constant="30" id="Sp4-fq-02j"/>
<constraint firstAttribute="width" constant="30" id="sw2-Jo-OJQ"/>
Expand All @@ -56,6 +64,7 @@
</connections>
</button>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="cim-ZE-7AD">
<rect key="frame" x="126" y="184.5" width="30" height="30"/>
<constraints>
<constraint firstAttribute="height" constant="30" id="PsJ-aG-kjp"/>
<constraint firstAttribute="width" constant="30" id="ugU-bC-Pae"/>
Expand All @@ -67,11 +76,13 @@
</connections>
</button>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Current tab" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="xz1-Jy-Azt">
<rect key="frame" x="144" y="156.5" width="87.5" height="20.5"/>
<fontDescription key="fontDescription" type="system" pointSize="17"/>
<color key="textColor" red="0.0" green="0.0" blue="0.0" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<nil key="highlightedColor"/>
</label>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="0" textAlignment="center" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="WCC-jO-kGE">
<rect key="frame" x="176" y="189" width="23" height="21"/>
<constraints>
<constraint firstAttribute="width" constant="23" id="g75-eX-p9k"/>
<constraint firstAttribute="height" constant="21" id="lDt-If-hQD"/>
Expand Down
68 changes: 52 additions & 16 deletions JKSteppedProgressBar/Classes/SteppedProgressBar.swift
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ import UIKit

public let SteppedProgressBarAutomaticDimension: CGFloat = -1

public enum StepDrawingMode: Int {
case fill
case drawIndex
// TODO:
case image
}

@IBDesignable
open class SteppedProgressBar: UIView {

Expand Down Expand Up @@ -39,6 +46,14 @@ open class SteppedProgressBar: UIView {
}
}

// Addressing issue #3
// https://github.com/jkmathew/JKSteppedProgressBar/issues/3
@IBInspectable open var titleOffset: CGFloat = 0 {
didSet {
self.setNeedsDisplay()
}
}

@IBInspectable open var circleSpacing: CGFloat = SteppedProgressBarAutomaticDimension {
didSet {
self.setNeedsDisplay()
Expand All @@ -57,6 +72,12 @@ open class SteppedProgressBar: UIView {
}
}

open var stepDrawingMode: StepDrawingMode = .drawIndex {
didSet {
self.setNeedsDisplay()
}
}

private var actualSpacing: CGFloat {
return (circleSpacing == SteppedProgressBarAutomaticDimension) ? (frame.width - 6.0 - (CGFloat(titles.count) * circleRadius)) / CGFloat(titles.count - 1) : circleSpacing
}
Expand All @@ -77,24 +98,32 @@ open class SteppedProgressBar: UIView {
let context = UIGraphicsGetCurrentContext()

if currentTab == 0 {
_ = drawTabs(from: 0, to: count, color: inactiveColor, textColor: inactiveTextColor)
drawTabs(from: 0, to: count, color: inactiveColor, textColor: inactiveTextColor)
}
else if currentTab == count {
_ = drawTabs(from: 0, to: count, color: activeColor, textColor: activeColor)
drawTabs(from: 0, to: count, color: activeColor, textColor: activeColor)
}
else {
let first = drawTabs(from: 0, to: currentTab , color: activeColor, textColor: activeColor).end
let second = drawTabs(from: currentTab, to: count, color: inactiveColor, textColor: inactiveTextColor).start
// Addressing issue #3
// https://github.com/jkmathew/JKSteppedProgressBar/issues/3
// Drawing in the order 1.inactive, 2.Line between active and inactive, 3.Active to avoid overlaping issue
let end = drawTabs(from: currentTab, to: count, color: inactiveColor, textColor: inactiveTextColor).start
let path = UIBezierPath()
path.lineWidth = lineWidth

path.move(to: first)
path.addLine(to: second)
var start = end
start.x -= actualSpacing
path.move(to: start)
path.addLine(to: end)
context?.setStrokeColor(inactiveColor.cgColor)
path.stroke()

drawTabs(from: 0, to: currentTab , color: activeColor, textColor: activeColor)

}
}

@discardableResult
func drawTabs(from begin: Int, to end: Int, color: UIColor, textColor: UIColor) -> (start: CGPoint, end: CGPoint) {
let startX = bounds.midX - (CGFloat(titles.count - 1) * (actualSpacing + circleRadius) / 2.0)
let x = startX + (actualSpacing + circleRadius) * CGFloat(begin)
Expand All @@ -115,7 +144,11 @@ open class SteppedProgressBar: UIView {
}
let context = UIGraphicsGetCurrentContext()
context?.setStrokeColor(color.cgColor)
context?.setFillColor(color.cgColor)
path.stroke()
if stepDrawingMode == .fill {
path.fill()
}
return (start: start, end: point)
}

Expand All @@ -124,30 +157,33 @@ open class SteppedProgressBar: UIView {
path.move(to: point)
let buttonRect = circleRect(point, radius: circleRadius)
let circlePath = UIBezierPath(ovalIn: buttonRect)
path.append(circlePath)

var attributes = [NSForegroundColorAttributeName : textColor, NSParagraphStyleAttributeName: paragraphStyle]

//draw index
let index = i
let buttonTitle = "\(index + 1)"
let font = UIFont.boldSystemFont(ofSize: 14.0)

var attributes = [NSForegroundColorAttributeName : textColor, NSParagraphStyleAttributeName: paragraphStyle, NSFontAttributeName: font]
let attributedButtonTitle = NSAttributedString(string: buttonTitle, attributes: attributes)
drawString(attributedButtonTitle, center: point)
if stepDrawingMode == .drawIndex {
let buttonTitle = "\(index + 1)"
let font = UIFont.boldSystemFont(ofSize: 14.0)
attributes[NSFontAttributeName] = font
let attributedButtonTitle = NSAttributedString(string: buttonTitle, attributes: attributes)
draw(string: attributedButtonTitle, center: point)
}
path.append(circlePath)

var titleCenter = point
titleCenter.y += circleRadius * 0.75
titleCenter.y += circleRadius * 0.75 + titleOffset
let title = titles[index]
attributes[NSFontAttributeName] = UIFont.boldSystemFont(ofSize: 12.0)
let attributedTitle = NSAttributedString(string: title, attributes: attributes)
drawString(attributedTitle, center: titleCenter)
draw(string: attributedTitle, center: titleCenter)

point.x += circleRadius / 2.0
path.move(to: point)

}

func drawString(_ string: NSAttributedString, center: CGPoint) {
func draw(string: NSAttributedString, center: CGPoint) {
var rect = string.boundingRect(with: CGSize(width: 1000, height: 1000), options: .usesFontLeading, context: nil)
let size = rect.size
let origin = CGPoint(x: center.x - size.width / 2.0, y: center.y - size.height / 2.0)
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ To use JKSteppedProgressBar Xcode 8.0 or later is required
JKSteppedProgressBar is available through [CocoaPods](http://cocoapods.org). To install
it, simply add the following line to your Podfile:

## TODO
- [ ] Respect language direction for drawing
- [ ] Add image for steps

```ruby
pod 'JKSteppedProgressBar'
```
Expand Down

0 comments on commit 68eb124

Please sign in to comment.