33// CDCodabarViewSample
44//
55// Created by Cole Dunsby on 2015-12-21.
6- // Copyright © 2015 Cole Dunsby. All rights reserved.
6+ // Copyright © 2016 Cole Dunsby. All rights reserved.
77//
88
99/*********************************************************************
@@ -69,29 +69,13 @@ public class CDCodabarView: UIView {
6969 " D " : " 1010011001 "
7070 ]
7171
72- @IBInspectable public var code : String = " A123456789B " {
73- didSet { setNeedsDisplay ( ) }
74- }
75-
76- @IBInspectable public var barColor : UIColor = . blackColor( ) {
77- didSet { setNeedsDisplay ( ) }
78- }
79-
80- @IBInspectable public var textColor : UIColor = . blackColor( ) {
81- didSet { setNeedsDisplay ( ) }
82- }
72+ @IBInspectable public var code : String = " A123456789B " { didSet { setNeedsDisplay ( ) } }
73+ @IBInspectable public var barColor : UIColor = . black { didSet { setNeedsDisplay ( ) } }
74+ @IBInspectable public var textColor : UIColor = . black { didSet { setNeedsDisplay ( ) } }
75+ @IBInspectable public var padding : CGFloat = 2.0 { didSet { setNeedsDisplay ( ) } }
76+ @IBInspectable public var hideCode : Bool = false { didSet { setNeedsDisplay ( ) } }
8377
84- @IBInspectable public var padding : CGFloat = 2.0 {
85- didSet { setNeedsDisplay ( ) }
86- }
87-
88- @IBInspectable public var hideCode : Bool = false {
89- didSet { setNeedsDisplay ( ) }
90- }
91-
92- public var font = UIFont ( name: " AvenirNext-Regular " , size: 15.0 ) ! {
93- didSet { setNeedsDisplay ( ) }
94- }
78+ public var font = UIFont ( name: " AvenirNext-Regular " , size: 15.0 ) ! { didSet { setNeedsDisplay ( ) } }
9579
9680 override init ( frame: CGRect ) {
9781 super. init ( frame: frame)
@@ -101,47 +85,47 @@ public class CDCodabarView: UIView {
10185 super. init ( coder: aDecoder)
10286 }
10387
104- override public func drawRect ( rect: CGRect ) {
105- let paragraphStyle = NSParagraphStyle . defaultParagraphStyle ( ) . mutableCopy ( ) as! NSMutableParagraphStyle
106- paragraphStyle. alignment = NSTextAlignment . Center
88+ override public func draw ( _ rect: CGRect ) {
89+ let paragraphStyle = NSParagraphStyle . default . mutableCopy ( ) as! NSMutableParagraphStyle
90+ paragraphStyle. alignment = . center
10791
10892 let attributes = [
10993 NSFontAttributeName: font,
11094 NSForegroundColorAttributeName: textColor,
11195 NSParagraphStyleAttributeName: paragraphStyle,
112- ]
96+ ] as [ String : Any ]
11397
114- if !codeIsValid ( ) {
98+ if !isCodeValid ( ) {
11599 let text = " Invalid Code "
116- let textSize = text. boundingRectWithSize ( CGSize ( width: bounds. size. width, height: CGFloat . max ) , options: [ . TruncatesLastVisibleLine , . UsesLineFragmentOrigin ] , attributes: [ NSFontAttributeName: font] , context: nil )
100+ let textSize = text. boundingRect ( with : CGSize ( width: bounds. size. width, height: CGFloat . greatestFiniteMagnitude ) , options: [ . truncatesLastVisibleLine , . usesLineFragmentOrigin ] , attributes: [ NSFontAttributeName: font] , context: nil )
117101
118- text. drawAtPoint ( CGPoint ( x: bounds. size. width / 2 - textSize. width / 2 , y: bounds. size. height / 2 - textSize. height / 2 ) , withAttributes: attributes)
102+ text. draw ( at : CGPoint ( x: bounds. size. width / 2 - textSize. width / 2 , y: bounds. size. height / 2 - textSize. height / 2 ) , withAttributes: attributes)
119103
120104 return
121105 }
122106
123107 barColor. setFill ( )
124108
125109 let multiplier : CGFloat = 1.25
126- let labelHeight = ceil ( code. boundingRectWithSize ( CGSize ( width: bounds. size. width, height: CGFloat . max ) , options: [ . TruncatesLastVisibleLine , . UsesLineFragmentOrigin ] , attributes: [ NSFontAttributeName: font] , context: nil ) . height)
110+ let labelHeight = ceil ( code. boundingRect ( with : CGSize ( width: bounds. size. width, height: CGFloat . greatestFiniteMagnitude ) , options: [ . truncatesLastVisibleLine , . usesLineFragmentOrigin ] , attributes: [ NSFontAttributeName: font] , context: nil ) . height)
127111 let barHeight = bounds. size. height - ( hideCode ? 0 : labelHeight + padding)
128112 let sequence = barSequence ( )
129113
130114 var narrow = 0
131115 var wide = 0
132116
133- for var i = 0 ; i < sequence. characters. count; i ++ {
117+ for i in 0 .. < sequence. characters. count {
134118 if sequence [ i] == " 0 " {
135- narrow++
119+ narrow += 1
136120 } else {
137121 if i < sequence. characters. count - 1 {
138122 if sequence [ i + 1 ] == " 1 " {
139- wide++
123+ wide += 1
140124 } else {
141- narrow++
125+ narrow += 1
142126 }
143127 } else {
144- narrow++
128+ narrow += 1
145129 }
146130 }
147131 }
@@ -150,7 +134,7 @@ public class CDCodabarView: UIView {
150134
151135 var x : CGFloat = 0.0
152136
153- for var i = 0 ; i < sequence. characters. count; i ++ {
137+ for i in 0 .. < sequence. characters. count {
154138 if sequence [ i] == " 0 " {
155139 x += barWidth
156140 } else {
@@ -169,14 +153,14 @@ public class CDCodabarView: UIView {
169153 }
170154
171155 if !hideCode {
172- ( code as NSString ) . drawInRect ( CGRect ( x: 0 , y: barHeight + padding, width: x, height: labelHeight) , withAttributes: attributes)
156+ ( code as NSString ) . draw ( in : CGRect ( x: 0 , y: barHeight + padding, width: x, height: labelHeight) , withAttributes: attributes)
173157 }
174158 }
175159
176160 private func barSequence( ) -> String {
177161 var barSequence = " "
178162
179- for var i = 0 ; i < code. characters. count; i ++ {
163+ for i in 0 .. < code. characters. count {
180164 barSequence += barcodeEncoding [ String ( code [ i] ) ] !
181165 if i < code. characters. count - 1 {
182166 barSequence += " 0 "
@@ -186,53 +170,46 @@ public class CDCodabarView: UIView {
186170 return barSequence
187171 }
188172
189- private func codeIsValid( ) -> Bool {
190- let validLength = code. characters. count >= 3 && code. characters. count <= 16
191- var validStart = true
192- var validStop = true
193- var validMiddle = true
194-
195- if validLength {
196- let start = code [ 0 ] . toUpper ( )
197- let stop = code [ code. characters. count - 1 ] . toUpper ( )
198- let middle = String ( code. characters. dropFirst ( ) . dropLast ( ) )
199-
200- validStart = start >= " A " && start <= " D "
201- validStop = stop >= " A " && stop <= " D "
202-
203- for char in middle. characters {
204- if !barcodeEncoding. keys. contains ( String ( char) ) {
205- validMiddle = false
206- break
207- }
173+ private func isCodeValid( ) -> Bool {
174+ guard code. characters. count >= 3 && code. characters. count <= 16 else { return false }
175+
176+ let startChar = code [ 0 ] . toUpper ( )
177+ let stopChar = code [ code. characters. count - 1 ] . toUpper ( )
178+ let middleString = String ( code. characters. dropFirst ( ) . dropLast ( ) )
179+
180+ let isValidStart = startChar >= " A " && startChar <= " D "
181+ let isValidStop = stopChar >= " A " && stopChar <= " D "
182+ var isValidMiddle = true
183+
184+ for char in middleString. characters {
185+ if !barcodeEncoding. keys. contains ( String ( char) ) {
186+ isValidMiddle = false
187+ break
208188 }
209189 }
210190
211- return validLength && validStart && validStop && validMiddle
191+ return isValidStart && isValidStop && isValidMiddle
212192 }
213-
214193}
215194
216- extension String {
195+ private extension String {
217196
218197 subscript ( i: Int ) -> Character {
219- return self [ self . startIndex . advancedBy ( i) ]
198+ return self [ self . characters . index ( self . startIndex , offsetBy : i) ]
220199 }
221200
222201 subscript ( i: Int ) -> String {
223202 return String ( self [ i] as Character )
224203 }
225204
226205 subscript ( r: Range < Int > ) -> String {
227- return substringWithRange ( Range ( start : startIndex . advancedBy ( r . startIndex) , end : startIndex . advancedBy ( r . endIndex ) ) )
206+ return substring ( with : ( characters . index ( startIndex, offsetBy : r . lowerBound ) ..< characters . index ( startIndex , offsetBy : r . upperBound ) ) )
228207 }
229-
230208}
231209
232- extension Character {
210+ private extension Character {
233211
234212 func toUpper( ) -> Character {
235- return String ( self ) . uppercaseString . characters. first!
213+ return String ( self ) . uppercased ( ) . characters. first!
236214 }
237-
238215}
0 commit comments