@@ -10,6 +10,13 @@ import UIKit
10
10
11
11
public let SteppedProgressBarAutomaticDimension : CGFloat = - 1
12
12
13
+ public enum StepDrawingMode : Int {
14
+ case fill
15
+ case drawIndex
16
+ // TODO:
17
+ case image
18
+ }
19
+
13
20
@IBDesignable
14
21
open class SteppedProgressBar : UIView {
15
22
@@ -39,6 +46,14 @@ open class SteppedProgressBar: UIView {
39
46
}
40
47
}
41
48
49
+ // Addressing issue #3
50
+ // https://github.com/jkmathew/JKSteppedProgressBar/issues/3
51
+ @IBInspectable open var titleOffset : CGFloat = 0 {
52
+ didSet {
53
+ self . setNeedsDisplay ( )
54
+ }
55
+ }
56
+
42
57
@IBInspectable open var circleSpacing : CGFloat = SteppedProgressBarAutomaticDimension {
43
58
didSet {
44
59
self . setNeedsDisplay ( )
@@ -57,6 +72,12 @@ open class SteppedProgressBar: UIView {
57
72
}
58
73
}
59
74
75
+ open var stepDrawingMode : StepDrawingMode = . drawIndex {
76
+ didSet {
77
+ self . setNeedsDisplay ( )
78
+ }
79
+ }
80
+
60
81
private var actualSpacing : CGFloat {
61
82
return ( circleSpacing == SteppedProgressBarAutomaticDimension) ? ( frame. width - 6.0 - ( CGFloat ( titles. count) * circleRadius) ) / CGFloat( titles. count - 1 ) : circleSpacing
62
83
}
@@ -77,24 +98,32 @@ open class SteppedProgressBar: UIView {
77
98
let context = UIGraphicsGetCurrentContext ( )
78
99
79
100
if currentTab == 0 {
80
- _ = drawTabs ( from: 0 , to: count, color: inactiveColor, textColor: inactiveTextColor)
101
+ drawTabs ( from: 0 , to: count, color: inactiveColor, textColor: inactiveTextColor)
81
102
}
82
103
else if currentTab == count {
83
- _ = drawTabs ( from: 0 , to: count, color: activeColor, textColor: activeColor)
104
+ drawTabs ( from: 0 , to: count, color: activeColor, textColor: activeColor)
84
105
}
85
106
else {
86
- let first = drawTabs ( from: 0 , to: currentTab , color: activeColor, textColor: activeColor) . end
87
- let second = drawTabs ( from: currentTab, to: count, color: inactiveColor, textColor: inactiveTextColor) . start
107
+ // Addressing issue #3
108
+ // https://github.com/jkmathew/JKSteppedProgressBar/issues/3
109
+ // Drawing in the order 1.inactive, 2.Line between active and inactive, 3.Active to avoid overlaping issue
110
+ let end = drawTabs ( from: currentTab, to: count, color: inactiveColor, textColor: inactiveTextColor) . start
88
111
let path = UIBezierPath ( )
89
112
path. lineWidth = lineWidth
90
113
91
- path. move ( to: first)
92
- path. addLine ( to: second)
114
+ var start = end
115
+ start. x -= actualSpacing
116
+ path. move ( to: start)
117
+ path. addLine ( to: end)
93
118
context? . setStrokeColor ( inactiveColor. cgColor)
94
119
path. stroke ( )
120
+
121
+ drawTabs ( from: 0 , to: currentTab , color: activeColor, textColor: activeColor)
122
+
95
123
}
96
124
}
97
125
126
+ @discardableResult
98
127
func drawTabs( from begin: Int , to end: Int , color: UIColor , textColor: UIColor ) -> ( start: CGPoint , end: CGPoint ) {
99
128
let startX = bounds. midX - ( CGFloat ( titles. count - 1 ) * ( actualSpacing + circleRadius) / 2.0 )
100
129
let x = startX + ( actualSpacing + circleRadius) * CGFloat( begin)
@@ -115,7 +144,11 @@ open class SteppedProgressBar: UIView {
115
144
}
116
145
let context = UIGraphicsGetCurrentContext ( )
117
146
context? . setStrokeColor ( color. cgColor)
147
+ context? . setFillColor ( color. cgColor)
118
148
path. stroke ( )
149
+ if stepDrawingMode == . fill {
150
+ path. fill ( )
151
+ }
119
152
return ( start: start, end: point)
120
153
}
121
154
@@ -124,30 +157,33 @@ open class SteppedProgressBar: UIView {
124
157
path. move ( to: point)
125
158
let buttonRect = circleRect ( point, radius: circleRadius)
126
159
let circlePath = UIBezierPath ( ovalIn: buttonRect)
127
- path. append ( circlePath)
128
160
161
+ var attributes = [ NSForegroundColorAttributeName : textColor, NSParagraphStyleAttributeName: paragraphStyle]
162
+
129
163
//draw index
130
164
let index = i
131
- let buttonTitle = " \( index + 1 ) "
132
- let font = UIFont . boldSystemFont ( ofSize: 14.0 )
133
-
134
- var attributes = [ NSForegroundColorAttributeName : textColor, NSParagraphStyleAttributeName: paragraphStyle, NSFontAttributeName: font]
135
- let attributedButtonTitle = NSAttributedString ( string: buttonTitle, attributes: attributes)
136
- drawString ( attributedButtonTitle, center: point)
165
+ if stepDrawingMode == . drawIndex {
166
+ let buttonTitle = " \( index + 1 ) "
167
+ let font = UIFont . boldSystemFont ( ofSize: 14.0 )
168
+ attributes [ NSFontAttributeName] = font
169
+ let attributedButtonTitle = NSAttributedString ( string: buttonTitle, attributes: attributes)
170
+ draw ( string: attributedButtonTitle, center: point)
171
+ }
172
+ path. append ( circlePath)
137
173
138
174
var titleCenter = point
139
- titleCenter. y += circleRadius * 0.75
175
+ titleCenter. y += circleRadius * 0.75 + titleOffset
140
176
let title = titles [ index]
141
177
attributes [ NSFontAttributeName] = UIFont . boldSystemFont ( ofSize: 12.0 )
142
178
let attributedTitle = NSAttributedString ( string: title, attributes: attributes)
143
- drawString ( attributedTitle, center: titleCenter)
179
+ draw ( string : attributedTitle, center: titleCenter)
144
180
145
181
point. x += circleRadius / 2.0
146
182
path. move ( to: point)
147
183
148
184
}
149
185
150
- func drawString ( _ string: NSAttributedString , center: CGPoint ) {
186
+ func draw ( string: NSAttributedString , center: CGPoint ) {
151
187
var rect = string. boundingRect ( with: CGSize ( width: 1000 , height: 1000 ) , options: . usesFontLeading, context: nil )
152
188
let size = rect. size
153
189
let origin = CGPoint ( x: center. x - size. width / 2.0 , y: center. y - size. height / 2.0 )
0 commit comments