How to provide default Codable in Swift

Issue #851 Use DefaultValue to provide defaultValue in our property wrapper DefaultCodable public protocol DefaultValue { associatedtype Value: Codable static var defaultValue: Value { get } } public enum DefaultBy { public enum True: DefaultValue { public static let defaultValue = true } public enum False: DefaultValue { public static let defaultValue = false } } @propertyWrapper public struct DefaultCodable<T: DefaultValue> { public var wrappedValue: T.Value public init(wrappedValue: T.Value) { self....

October 23, 2021 · 1 min · 143 words · Khoa

How to use Picker with optional selection in SwiftUI

Issue #849 We need to explicitly specify optional in tag extension AVCaptureDevice: Identifiable { public var id: String { uniqueID } } @State var device: AVCaptureDevice? Picker("Camera", selection: $device) { ForEach(manager.devices) { d in Text(d.localizedName) .tag(AVCaptureDevice?.some(d)) } }

September 30, 2021 · 1 min · 38 words · Khoa

How to scale system font size to support Dynamic Type

Issue #847 We should use Dynamic Font Type as much as possible, as per Typography guide and https://www.iosfontsizes.com/ But in case we have to use a specific font, we can scale it with UIFontMetrics import SwiftUI import UIKit extension Font { static func system( scaledSize size: CGFloat, weight: Font.Weight = .regular, design: Font.Design = .default ) -> Font { Font.system( size: UIFontMetrics.default.scaledValue(for: size), weight: weight, design: design ) } } Then instead of...

September 2, 2021 · 1 min · 130 words · Khoa

How to show suffix text in TextField in SwiftUI

Issue #845 Suppose we have struct Payment as the state, we declare custom Binding<String> that derives from our state. struct Payment { var amount: Int = 0 } To show our suffix view, we use .fixedSize(horizontal: true, vertical: false) to constrain our TextField to fit content, and to avoid it to grow outside our frame, we constrain a max limit, like 10_000 in this case. An interesting thing is we divide by 10 to not let the newly typed digit enter if the amount is above our limit...

August 24, 2021 · 1 min · 201 words · Khoa

How to show currency symbol in TextField in SwiftUI

Issue #844 Use custom Binding and validate the text. In case of zero, return empty string to let TextField display placeholder var textField: some View { let text = Binding<String>( get: { state.amount > 0 ? "$\(state.amount)" : "" }, set: { text in let text = text.replacingOccurrences(of: "$", with: "") state.amount = Int(text) ?? 0 } ) return TextField("$0", text: text) .keyboardType(.numberPad) .font(.system(size: 50, weight: .medium, design: .monospaced)) .foregroundColor(Asset.textPrimary.color) ....

August 24, 2021 · 1 min · 196 words · Khoa

How to read safe area insets in SwiftUI

Issue #842 Declare EnvironmentKey and read safeAreaInsets from key window in connectedScenes struct SafeAreaInsetsKey: EnvironmentKey { static var defaultValue: EdgeInsets { UIApplication.shared.keyWindow?.safeAreaInsets.swiftUIInsets ?? EdgeInsets() } } private extension UIEdgeInsets { var swiftUIInsets: EdgeInsets { EdgeInsets(top: top, leading: left, bottom: bottom, trailing: right) } } private extension UIApplication { var keyWindow: UIWindow? { connectedScenes .compactMap { $0 as? UIWindowScene } .flatMap { $0.windows } .first { $0.isKeyWindow } } } struct MessageBar: View { @Environment(\....

August 20, 2021 · 1 min · 78 words · Khoa

How to make tab strip with enum cases in SwiftUI

Issue #841 Declare generic on RawRepresentable import SwiftUI struct TabStrip<T: RawRepresentable & Hashable>: View where T.RawValue == String { let values: [T] @Binding var selected: T var body: some View { ScrollView(.horizontal, showsIndicators: false) { LazyHStack(spacing: 24) { ForEach(values, id: \.self) { value in Button(action: { selected = value }) { Text(value.rawValue) .foregroundColor(selected == value ? Asset.textPrimary.color : Asset.textSecondary.color) .fontWeight(selected == value ? .semibold : .regular) .padding(6) } .buttonStyle(PlainButtonStyle()) } } } ....

August 19, 2021 · 1 min · 115 words · Khoa

How to replace multiple regex matches in Swift

Issue #840 Used to replace credit card regex 30[0-5]#-######-###L in EasyFake We can use ? to have non-greedy behavior, or I here use square bracket to fine-tune the expression \{[\d*,]*\d*\} Also, I need to reversed the matches because each replacement makes the next range incorrect extension String { func replacingHash() -> String { self .map { c in guard c == "#" else { return String(c) } let number = Int....

August 11, 2021 · 1 min · 186 words · Khoa

How to move files with Swift script

Issue #839 Use locales data from faker.js to https://github.com/onmyway133/EasyFake, renaming files since files, regardless of sub directories in Xcode, must have different name. We use enumerator API on FileManager to traverse all files in all sub-directories import Foundation let localesURL = URL(fileURLWithPath: FileManager.default.currentDirectoryPath) .appendingPathComponent("Sources/EasyFake/Resources/locales") let manager = FileManager.default manager.enumerator(atPath: localesURL.path)?.forEach { path in guard let path = path as? String else { return } let url = URL(fileURLWithPath: path, relativeTo: localesURL) guard url....

August 10, 2021 · 1 min · 134 words · Khoa

How to map Binding with optional value in SwiftUI

Issue #838 We can write our custom Binding import SwiftUI extension Binding where Value == Date? { func flatten(defaultValue: Date) -> Binding<Date> { Binding<Date>( get: { wrappedValue ?? defaultValue }, set: { wrappedValue = $0 } ) } } Then use in places where it needs Binding with non-optional value DatePicker( "", selection: $date.flatten(defaultValue: Date()), displayedComponents: .hourAndMinute ) .datePickerStyle(WheelDatePickerStyle())

August 10, 2021 · 1 min · 59 words · Khoa

How to get view height in SwiftUI

Issue #837 I usually use GeometryReader in background to get size of view, and encapsulate it inside a ViewModifier struct GetHeightModifier: ViewModifier { @Binding var height: CGFloat func body(content: Content) -> some View { content.background( GeometryReader { geo -> Color in DispatchQueue.main.async { height = geo.size.height } return Color.clear } ) } } This is useful to constrain NavigationView height when in use inside a bottom sheet struct FilterView: View { @State private var height: CGFloat = 0 var body: some View { BottomSheet { NavigationView { VStack { Text("content") } ....

August 6, 2021 · 1 min · 101 words · Khoa

How to prevent wheel Picker conflict with DragGesture in SwiftUI

Issue #836 If we have a Picker inside a View that has DragGesture, chances are when we scroll the wheel, the DragGesture is detected too To prevent this, we can attach a dummy DragGesture to our Picker Picker("", selection: $number) { ForEach(FavoriteNumbers.allCases) { number in Text(number.name) } } .pickerStyle(WheelPickerStyle()) .gesture(DragGesture())

August 5, 2021 · 1 min · 50 words · Khoa

How to make equal width buttons in SwiftUI

Issue #835 I usually define ButtonStyle to encapsulate common styles related to buttons. Here we specify .frame(maxWidth: .infinity) to let this button take the whole width as possible struct MyActionButtonStyle: ButtonStyle { func makeBody(configuration: Configuration) -> some View { configuration.label .font(.headline.bold()) .foregroundColor(.white) .frame(height: 44) .frame(maxWidth: .infinity) .background(Color.green) .cornerRadius(8) } } If we have 2 buttons both specifying maxWidth: .infinity then they will be divided equally with a spacing specified by our HStack...

August 5, 2021 · 1 min · 113 words · Khoa

How to make bottom sheet in SwiftUI

Issue #834 In iOS 15, we can use UISheetPresentationController to show bottom sheet like native Maps app. But before that there’s no such built in bottom sheet in UIKit or SwiftUI. We can start defining API for it. There are 3 ways to show overlay content in SwiftUI fullScreenCover: this presents like a modal view controller bottom up offset: initially hide our bottom sheet overlay: show overlay full screen so we can have custom control....

August 5, 2021 · 2 min · 423 words · Khoa

How to show abbreviated ago Date in Swift

Issue #833 Use RelativeDateTimeFormatter. This assumes US locale is used extension Date { private static let relativeFormatter: RelativeDateTimeFormatter = { let formatter = RelativeDateTimeFormatter() formatter.calendar = Calendar(identifier: .gregorian) formatter.calendar?.locale = Locale(identifier: "en_US_POSIX") formatter.dateTimeStyle = .numeric formatter.unitsStyle = .abbreviated return formatter }() var ago: String { guard abs(timeIntervalSinceNow) > 60 else { return "Just Now" } let text = Self.relativeFormatter.localizedString(for: self, relativeTo: Date()) return text .replacingOccurrences(of: "ago", with: "") .replacingOccurrences(of: "min", with: "m") ....

August 4, 2021 · 1 min · 194 words · Khoa

How to bind to nested ObservableObject in SwiftUI

Issue #831 We can normally listen to sub ObservableObject by either listen to its objectWillChange or its Publisher class SubModel: ObservableObject { @Published var count = 0 } class AppModel: ObservableObject { @Published var submodel: SubModel = SubModel() private var bag = Set<AnyCancellable>() init() { submodel.$count .sink(receiveValue: { [weak self] _ in self?.objectWillChange.send() }) } } We can then make a convenient extension to ease this. We use DispatchQueue to trigger objectWillChange in another run loop, to achieve didSet behavior...

July 27, 2021 · 1 min · 139 words · Khoa

How to track contentSize and scroll offset for ScrollView in SwiftUI

Issue #829 Use PreferenceKey with a custom coordinateSpace to make our own TrackableScrollView. Note that the offset here is in reversed to contentOffset in normal UIScrollView import SwiftUI struct TrackableScrollView<Content: View>: View { @ViewBuilder let content: (ScrollViewProxy) -> Content let onContentSizeChange: (CGSize) -> Void let onOffsetChange: (CGPoint) -> Void var body: some View { ScrollViewReader { reader in ScrollView(showsIndicators: false) { GeometryReader { geo in Color.clear.preference( key: ScrollOffsetKey.self, value: geo.frame(in: ....

July 25, 2021 · 1 min · 134 words · Khoa

How to focus TextField in SwiftUI

Issue #828 From iOS 15, FocusState Use focused(_:) for single TextField, and focused(_:equals:) for multiple TextField struct FormView: View { @FocusState private var isFocused: Bool @State private var name = "" var body: some View { TextField("Name", text: $name) .focused($isFocused) } } struct FormView: View { enum Field: Hashable { case usernameField case passwordField } @State private var username = "" @State private var password = "" @FocusState private var focusedField: Field?...

July 23, 2021 · 1 min · 205 words · Khoa

How to use debounce in Combine in Swift

Issue #827 I’m trying a simple Future and sink. As long as I have debounce, only receiveCompletion gets called, but not receiveValue private var bag = Set<AnyCancellable>() let future = Future<Int, Never> { promise in promise(.success(1)) } future .debounce(for: .seconds(0.5), scheduler: DispatchQueue.main) .sink( receiveCompletion: { print($0) // Called }, receiveValue: { print($0) // Not called } ) .store(in: &bag) Reading debounce Publishes elements only after a specified time interval elapses between events....

July 21, 2021 · 1 min · 120 words · Khoa

How to use Button inside NavigationLink in SwiftUI

Issue #826 Use isActive binding @State private var goesToDetail: Bool = false NavigationLink( destination: DetailView(viewModel: viewModel), isActive: $goesToDetail) { Button(action: { goesToDetail = true }) { Text("Next") } .buttonStyle(MyButtonStyle)) } We can also apply buttonStyle onto NavigationLink NavigationLink { FavoritesView() } label: { Image(systemImage: "star.fill") } .buttonStyle(MyButtonStyle())

July 21, 2021 · 1 min · 47 words · Khoa