forked from arthurhammer/FrameGrabber
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ButtonStyle.swift
90 lines (77 loc) · 3.34 KB
/
ButtonStyle.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
import UIKit
extension Style {
static let defaultButtonCornerRadius: CGFloat = 16
static let mediumButtonCornerRadius: CGFloat = 12
}
extension UIButton.Configuration {
static func action(using baseConfiguration: Self = .filled()) -> Self {
var configuration = baseConfiguration
configuration.setDefaultContentInsets()
configuration.buttonSize = .large
configuration.cornerStyle = .fixed
configuration.background.cornerRadius = Style.defaultButtonCornerRadius
configuration.baseBackgroundColor = .accent
configuration.imagePadding = 8
let font = UIFont.preferredFont(forTextStyle: .headline)
configuration.titleTextAttributesTransformer = .init { attributes in
var attributes = attributes
attributes.font = font
return attributes
}
return configuration
}
static func secondaryAction() -> Self {
action(using: .tinted())
}
static func actionAccessory(using baseConfiguration: Self = .plain()) -> Self {
var configuration = baseConfiguration
configuration.setDefaultContentInsets()
configuration.baseForegroundColor = .secondaryLabel
configuration.imagePadding = 8
let font = UIFont.preferredFont(forTextStyle: .subheadline, weight: .medium)
configuration.titleTextAttributesTransformer = .init { attributes in
var attributes = attributes
attributes.font = font
return attributes
}
return configuration
}
}
// MARK: - Deprecated
@available(iOS, deprecated: 16, message: "Use `UIButton.Configuration`")
extension UIButton {
static func action(withHeight height: CGFloat? = 50, minimumWidth: CGFloat? = nil) -> UIButton {
let button = UIButton(type: .system)
button.configureAsActionButton(withHeight: height, minimumWidth: minimumWidth)
return button
}
func configureAsActionButton(withHeight height: CGFloat? = 50, minimumWidth: CGFloat? = nil) {
if let height {
let constraint = heightAnchor.constraint(equalToConstant: height)
constraint.priority = .required - 1
constraint.isActive = true
}
if let minimumWidth {
let constraint = widthAnchor.constraint(greaterThanOrEqualToConstant: minimumWidth)
constraint.priority = .required - 1
constraint.isActive = true
}
contentEdgeInsets = UIEdgeInsets(top: 0, left: 16, bottom: 0, right: 16)
layer.cornerRadius = Style.defaultButtonCornerRadius
layer.cornerCurve = .continuous
titleLabel?.font = .preferredFont(forTextStyle: .headline)
configureDynamicTypeLabel()
}
func configureDynamicTypeLabel() {
titleLabel?.adjustsFontForContentSizeCategory = true
titleLabel?.adjustsFontSizeToFitWidth = true
titleLabel?.minimumScaleFactor = 0.6
titleLabel?.allowsDefaultTighteningForTruncation = true
titleLabel?.lineBreakMode = .byTruncatingTail
}
func configureTrailingAlignedImage() {
// Hack to flip the image to the right side.
let isRightToLeft = UIApplication.shared.userInterfaceLayoutDirection == .rightToLeft
semanticContentAttribute = isRightToLeft ? .forceLeftToRight : .forceRightToLeft
}
}