-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTools.swift
156 lines (132 loc) · 4.6 KB
/
Tools.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
//
// Tools.swift
// WeOut
//
// Created by Jonathan Loving on 1/26/24.
//
import Foundation
import SwiftUI
/*extension Color {
init(hex: Int, opacity: Double = 1.0) {
let red = Double((hex & 0xff0000) >> 16) / 255.0
let green = Double((hex & 0xff00) >> 8) / 255.0
let blue = Double((hex & 0xff) >> 0) / 255.0
self.init(.sRGB, red: red, green: green, blue: blue, opacity: opacity)
}
}*/
extension Color {
init?(hex: String) {
guard let uiColor = UIColor(hex: hex) else { return nil }
self.init(uiColor: uiColor)
}
func toHexString(includeAlpha: Bool = false) -> String? {
return UIColor(self).toHexString(includeAlpha: includeAlpha)
}
}
//Reference for creating hexidecimal colors > https://ditto.live/blog/swift-hex-color-extension
extension UIColor {
// Initializes a new UIColor instance from a hex string
convenience init?(hex: String) {
var hexString = hex.trimmingCharacters(in: .whitespacesAndNewlines).uppercased()
if hexString.hasPrefix("#") {
hexString.removeFirst()
}
let scanner = Scanner(string: hexString)
var rgbValue: UInt64 = 0
guard scanner.scanHexInt64(&rgbValue) else {
return nil
}
var red, green, blue, alpha: UInt64
switch hexString.count {
case 6:
red = (rgbValue >> 16)
green = (rgbValue >> 8 & 0xFF)
blue = (rgbValue & 0xFF)
alpha = 255
case 8:
red = (rgbValue >> 16)
green = (rgbValue >> 8 & 0xFF)
blue = (rgbValue & 0xFF)
alpha = rgbValue >> 24
default:
return nil
}
self.init(red: CGFloat(red) / 255, green: CGFloat(green) / 255, blue: CGFloat(blue) / 255, alpha: CGFloat(alpha) / 255)
}
// Returns a hex string representation of the UIColor instance
func toHexString(includeAlpha: Bool = false) -> String? {
// Get the red, green, and blue components of the UIColor as floats between 0 and 1
guard let components = self.cgColor.components else {
// If the UIColor's color space doesn't support RGB components, return nil
return nil
}
// Convert the red, green, and blue components to integers between 0 and 255
let red = Int(components[0] * 255.0)
let green = Int(components[1] * 255.0)
let blue = Int(components[2] * 255.0)
// Create a hex string with the RGB values and, optionally, the alpha value
let hexString: String
if includeAlpha, let alpha = components.last {
let alphaValue = Int(alpha * 255.0)
hexString = String(format: "#%02X%02X%02X%02X", red, green, blue, alphaValue)
} else {
hexString = String(format: "#%02X%02X%02X", red, green, blue)
}
// Return the hex string
return hexString
}
}
struct TextFieldClearButton: ViewModifier {
@Binding var fieldText: String
func body(content: Content) -> some View {
content
.overlay {
if !fieldText.isEmpty {
HStack {
Spacer()
Button {
fieldText = ""
} label: {
Image(systemName: "multiply.circle.fill")
}
.foregroundColor(.secondary)
.padding(.trailing, 4)
}
}
}
}
}
extension View {
func showClearButton(_ text: Binding<String>) -> some View {
self.modifier(TextFieldClearButton(fieldText: text))
}
}
struct TextFieldButton: View {
@Binding var text: String
var textFieldExampleMessage: String
@FocusState private var isTextFieldFocused: Bool
var body: some View {
VStack {
TextField(textFieldExampleMessage, text: $text, axis: .vertical)
.textFieldStyle(.roundedBorder)
.focused($isTextFieldFocused)
.showClearButton($text)
.foregroundStyle(.text)
.font(.title2)
.tint(.blinkingLineCursor)
//.background(.white)
}
.padding()
//.background(Color.gray)
}
}
struct CompactDatePickerView: View {
@Binding var selectedDate: Date
var body: some View {
ZStack {
DatePicker("", selection: $selectedDate, displayedComponents: [.date])
.datePickerStyle(CompactDatePickerStyle())
.background(.date)
}
}
}