-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathPaddingLabel.swift
74 lines (59 loc) · 1.95 KB
/
PaddingLabel.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
//
// PaddingLabel.swift
// OBAKitCore
//
// Created by Alan Chu on 8/16/20.
//
import UIKit
public class PaddingLabel: UILabel {
// MARK: - Properties
public var insets: UIEdgeInsets
public var cornerRadius: CGFloat {
get { self.layer.cornerRadius }
set {
self.layer.cornerRadius = newValue
self.layer.masksToBounds = newValue > 0
}
}
override public var bounds: CGRect {
didSet {
// ensures this works within stack views if multi-line
preferredMaxLayoutWidth = bounds.width - (insets.left + insets.right)
}
}
// MARK: - Initializers
public init(insets: UIEdgeInsets) {
self.insets = insets
super.init(frame: .zero)
registerForTraitChanges()
}
override public init(frame: CGRect) {
self.insets = UIEdgeInsets(top: 4, left: 8, bottom: 4, right: 8)
super.init(frame: frame)
registerForTraitChanges()
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
// MARK: - Layout
override public func drawText(in rect: CGRect) {
super.drawText(in: rect.inset(by: insets))
}
override public var intrinsicContentSize: CGSize {
let size = super.intrinsicContentSize
return CGSize(width: size.width + insets.left + insets.right,
height: size.height + insets.top + insets.bottom)
}
func registerForTraitChanges() {
registerForTraitChanges([UITraitAccessibilityContrast.self]) { (self: Self, _) in
self.configure()
}
}
func configure() {
let isHighContrast = traitCollection.accessibilityContrast == .high
if let backgroundColor = self.backgroundColor {
self.layer.borderWidth = isHighContrast ? 4.0 : 0.0
self.layer.borderColor = isHighContrast ? backgroundColor.cgColor : UIColor.clear.cgColor
}
}
}