This repository has been archived by the owner on Oct 12, 2024. It is now read-only.
forked from Coledunsby/CDCodabarView
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCDCodabarView.swift
executable file
·124 lines (100 loc) · 4.04 KB
/
CDCodabarView.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
//
// CDCodabarView.swift
// CDCodabarViewSample
//
// Created by Cole Dunsby on 2015-12-21.
// Copyright © 2017 Cole Dunsby. All rights reserved.
//
import UIKit
@IBDesignable
public final class CDCodabarView: UIView {
@IBInspectable public var barColor: UIColor = #colorLiteral(red: 0, green: 0, blue: 0, alpha: 1) { didSet { setNeedsDisplay() }}
@IBInspectable public var textColor: UIColor = #colorLiteral(red: 0, green: 0, blue: 0, alpha: 1) { didSet { setNeedsDisplay() }}
@IBInspectable public var padding: CGFloat = 2.0 { didSet { setNeedsDisplay() }}
@IBInspectable public var hideCode: Bool = false { didSet { setNeedsDisplay() }}
@IBInspectable public var code: String = "A123456789B" {
didSet {
encoder = try? CDCodabarEncoder(code: code)
setNeedsDisplay()
}
}
public var font = UIFont.systemFont(ofSize: 15.0) { didSet { setNeedsDisplay() }}
private var encoder: CDCodabarEncoder?
override init(frame: CGRect) {
super.init(frame: frame)
}
required public init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override public func draw(_ rect: CGRect) {
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = .center
let attributes: [String: Any] = [
NSFontAttributeName: font,
NSForegroundColorAttributeName: textColor,
NSParagraphStyleAttributeName: paragraphStyle,
]
guard let encoder = encoder else {
let text = "Invalid Code"
let textSize = text.boundingRect(
with: CGSize(width: bounds.size.width, height: CGFloat.greatestFiniteMagnitude),
options: [.truncatesLastVisibleLine, .usesLineFragmentOrigin],
attributes: [NSFontAttributeName: font],
context: nil
)
text.draw(
at: CGPoint(x: bounds.size.width / 2 - textSize.width / 2, y: bounds.size.height / 2 - textSize.height / 2),
withAttributes: attributes
)
return
}
barColor.setFill()
let multiplier = CGFloat(1.25)
let labelHeight = ceil(code.boundingRect(
with: CGSize(width: bounds.size.width, height: CGFloat.greatestFiniteMagnitude),
options: [.truncatesLastVisibleLine, .usesLineFragmentOrigin],
attributes: [NSFontAttributeName: font],
context: nil).height)
let barHeight = bounds.size.height - (hideCode ? 0 : labelHeight + padding)
let sequence = encoder.sequence()
var narrow = 0
var wide = 0
(0 ..< sequence.count).forEach {
if sequence[$0] == 0 {
narrow += 1
} else {
if $0 < sequence.count - 1 {
if sequence[$0 + 1] == 1 {
wide += 1
} else {
narrow += 1
}
} else {
narrow += 1
}
}
}
let barWidth = CGFloat(bounds.size.width / (CGFloat(narrow) + multiplier * CGFloat(wide)))
var x = CGFloat(0)
(0 ..< sequence.count).forEach {
if sequence[$0] == 0 {
x += barWidth
} else {
var customBarWidth = barWidth
if $0 < sequence.count - 1 {
if sequence[$0 + 1] == 1 {
customBarWidth *= multiplier
}
}
UIRectFill(CGRect(x: x, y: 0, width: customBarWidth, height: barHeight))
x += customBarWidth
}
}
if !hideCode {
code.draw(
in: CGRect(x: 0, y: barHeight + padding, width: x, height: labelHeight),
withAttributes: attributes
)
}
}
}