This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #if targetEnvironment(simulator) | |
| let isRunningOnSimulator = true | |
| #else | |
| let isRunningOnSimulator = false | |
| #endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // From: https://www.avanderlee.com/swift/async-await/ | |
| // More info on @available: https://www.avanderlee.com/swift/available-deprecated-renamed/ | |
| @available(*, deprecated, renamed: "fetchImages()") | |
| func fetchImages(completion: @escaping (Result<[UIImage], Error>) -> Void) { ... } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Taken from Apple's App Dev Training: https://developer.apple.com/tutorials/app-dev-training/ | |
| /// This color is either black or white, whichever is more accessible when viewed against the scrum color. | |
| var accessibleFontColor: Color { | |
| var red: CGFloat = 0 | |
| var green: CGFloat = 0 | |
| var blue: CGFloat = 0 | |
| UIColor(self).getRed(&red, green: &green, blue: &blue, alpha: nil) | |
| return isLightColor(red: red, green: green, blue: blue) ? .black : .white | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| struct MyType { | |
| var property: String = "" { | |
| didSet { | |
| print("Property didSet: \(property)") | |
| } | |
| } | |
| init(property: String) { | |
| // self.property = property // this way didSet is not called on init |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import SwiftUI | |
| /// Note: Height is calculated dynamically and it is available only in run-time. To test it with Preview set a fixed size. | |
| struct AttributedText: View { | |
| @State private var textSize: CGSize = .zero | |
| var attributedString: NSAttributedString | |
| init(_ text: NSAttributedString) { | |
| attributedString = text | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| struct LoaderView<Content>: View where Content: View { | |
| var isShowing: Bool | |
| var content: () -> Content | |
| var body: some View { | |
| GeometryReader { geometry in | |
| ZStack(alignment: .center) { | |
| self.content() | |
| .disabled(self.isShowing) | |
| .blur(radius: self.isShowing ? 8 : 0) | |
| ActivityIndicator( |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| struct ContentView: View { | |
| @Environment(\.colorScheme) var colorScheme: ColorScheme | |
| var body: some View { | |
| self.preferencesButton | |
| } | |
| var preferencesButton: some View { | |
| Button(action: { | |
| self.navigator.presenting = .preferences | |
| }) { | |
| Image(systemName: “selection.pin.in.out”) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Credit: https://stackoverflow.com/questions/4421267/how-to-get-text-string-from-nth-line-of-uilabel/43284779 | |
| extension UILabel { | |
| /// creates an array containing one entry for each line of text the label has | |
| var lines: [String]? { | |
| guard let text = text, let font = font else { return nil } | |
| let attStr = NSMutableAttributedString(string: text) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // code from the book “Thinking in SwiftUI” by Chris Eidhof | |
| extension View { | |
| func badge(count: Int) -> some View { | |
| overlay( | |
| ZStack { | |
| if count != 0 { | |
| Circle() | |
| .fill(Color.red) | |
| Text("\(count)") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 1. xcrun simctl erase all | |
| 2. Restart computer!! |