How to mask with UILabel

Issue #603 Need to set correct frame for mask layer or UILabel, as it is relative to the coordinate of the view to be masked let aView = UIView(frame: .init(x: 100, y: 110, width: 200, height: 100)) let textLayer = CATextLayer() textLayer.foregroundColor = UIColor.white.cgColor textLayer.string = "Hello world" textLayer.font = UIFont.preferredFont(forTextStyle: .largeTitle) textLayer.frame = aView.bounds aView.layer.mask = textLayer Use sizeToFit to ensure frame for UILabel let label = UILabel() label.frame.origin = CGPoint(x: 80, y: 80) label....

February 13, 2020 · 1 min · 205 words · Khoa

How to avoid pitfalls in SwiftUI

Issue #602 Identify by unique id ForEach(store.blogs.enumerated().map({ $0 }), id: \.element.id) { index, blog in } ``` ##

February 12, 2020 · 1 min · 18 words · Khoa

How to use application will terminate in macOS

Issue #601 On Xcode 11, applicationWillTerminate is not called because of default automatic termination on in Info.plist. Removing NSSupportsSuddenTermination to trigger will terminate notification func applicationWillTerminate(_ notification: Notification) { save() } <key>NSSupportsAutomaticTermination</key> <true/> <key>NSSupportsSuddenTermination</key> <true/>

February 12, 2020 · 1 min · 35 words · Khoa

How to sync multiple CAAnimation

Issue #600 Use same CACurrentMediaTime final class AnimationSyncer { static let now = CACurrentMediaTime() func makeAnimation() -> CABasicAnimation { let animation = CABasicAnimation(keyPath: "opacity") animation.fillMode = .forwards animation.fromValue = 0 animation.toValue = 1 animation.repeatCount = .infinity animation.duration = 2 animation.beginTime = Self.now animation.autoreverses = true animation.timingFunction = CAMediaTimingFunction(name: .easeInEaseOut) return animation } }

February 11, 2020 · 1 min · 53 words · Khoa

How to use TabView with enum in SwiftUI

Issue #599 Specify tag enum Authentication: Int, Codable { case key case certificate } TabView(selection: $authentication) { KeyAuthenticationView() .tabItem { Text("🔑 Key") } .tag(Authentication.key) CertificateAuthenticationView() .tabItem { Text("📰 Certificate") } .tag(Authentication.certificate) }

February 11, 2020 · 1 min · 32 words · Khoa

How to build SwiftUI style UICollectionView data source in Swift

Issue #598 It’s hard to see any iOS app which don’t use UITableView or UICollectionView, as they are the basic and important foundation to represent data. UICollectionView is very basic to use, yet a bit tedious for common use cases, but if we abstract over it, then it becomes super hard to customize. Every app is unique, and any attempt to wrap around UICollectionView will fail horribly. A sensable approach for a good abstraction is to make it super easy for normal cases, and easy to customize for advanced scenarios....

February 9, 2020 · 4 min · 832 words · Khoa

How to make round border in SwiftUI

Issue #597 TextView(font: R.font.text!, lineCount: nil, text: $text, isFocus: $isFocus) .padding(8) .background(R.color.inputBackground) .cornerRadius(10) .overlay( RoundedRectangle(cornerRadius: 10) .stroke(isFocus ? R.color.inputBorderFocus : Color.clear, lineWidth: 1) )

February 5, 2020 · 1 min · 24 words · Khoa

How to mock in Swift

Issue #596 Unavailable init UNUserNotificationCenter.current().getNotificationSettings(completionHandler: { (settings: UNNotificationSettings) in let status: UNAuthorizationStatus = .authorized settings.setValue(status.rawValue, forKey: "authorizationStatus") completionHandler(settings) })

February 4, 2020 · 1 min · 19 words · Khoa

How to change background color in List in SwiftUI for macOS

Issue #595 SwiftUI uses ListCoreScrollView and ListCoreClipView under the hood. For now the workaround, is to avoid using List List { ForEach } use VStack { ForEach }

February 4, 2020 · 1 min · 28 words · Khoa

How to add drag and drop in SwiftUI

Issue #594 In some case, we should not use base type like UTType.text but to be more specific like UTType.utf8PlainText import UniformTypeIdentifiers var dropTypes: [UTType] { [ .fileURL, .url, .utf8PlainText, .text ] } func handleDrop(info: DropInfo) -> Bool { for provider in info.itemProviders(for: dropTypes) { for type in dropTypes { provider.loadDataRepresentation(forTypeIdentifier: type.identifier) { data, _ in guard let data = data, let string = String(data: data, encoding: .utf8) else { return } DispatchQueue....

February 4, 2020 · 2 min · 218 words · Khoa

How to weak link Combine in macOS 10.14 and iOS 12

Issue #593 #if canImport(Combine) is not enough, need to specify in Other Linker Flags OTHER_LDFLAGS = -weak_framework Combine Read more https://stackoverflow.com/questions/57168931/optional-linking-for-swift-combine-framework-in-xcode-11

February 2, 2020 · 1 min · 21 words · Khoa

How to make radio button group in SwiftUI

Issue #592 Use picker with Radio style Hard to customize Picker(selection: Binding<Bool>.constant(true), label: EmptyView()) { Text("Production").tag(0) Text("Sandbox").tag(1) }.pickerStyle(RadioGroupPickerStyle()) Use custom view Use contentShape to make whole button tappable. Make custom Binding for our enum struct EnvironmentView: View { @Binding var input: Input var body: some View { VStack(alignment: .leading) { RadioButton(text: "Production", isOn: binding(for: .production)) RadioButton(text: "Sandbox", isOn: binding(for: .sandbox)) } } private func binding(for environment: Input.Environment) -> Binding<Bool> { Binding<Bool>( get: { self....

February 1, 2020 · 1 min · 157 words · Khoa

How to set font to NSTextField in macOS

Issue #591 Use NSTextView instead

January 29, 2020 · 1 min · 5 words · Khoa

How to make borderless material NSTextField in SwiftUI for macOS

Issue #590 Use custom NSTextField as it is hard to customize TextFieldStyle import SwiftUI struct MaterialTextField: View { let placeholder: String @Binding var text: String @State var isFocus: Bool = false var body: some View { VStack(alignment: .leading, spacing: 0) { BorderlessTextField(placeholder: placeholder, text: $text, isFocus: $isFocus) .frame(maxHeight: 40) Rectangle() .foregroundColor(isFocus ? R.color.separatorFocus : R.color.separator) .frame(height: isFocus ? 2 : 1) } } } class FocusAwareTextField: NSTextField { var onFocusChange: (Bool) -> Void = { _ in } override func becomeFirstResponder() -> Bool { let textView = window?...

January 29, 2020 · 2 min · 231 words · Khoa

How to make focusable NSTextField in macOS

Issue #589 Use onTapGesture import SwiftUI struct MyTextField: View { @Binding var text: String let placeholder: String @State private var isFocus: Bool = false var body: some View { FocusTextField(text: $text, placeholder: placeholder, isFocus: $isFocus) .padding() .cornerRadius(4) .overlay( RoundedRectangle(cornerRadius: 4) .stroke(isFocus ? Color.accentColor: Color.separator) ) .onTapGesture { isFocus = true } } } private struct FocusTextField: NSViewRepresentable { @Binding var text: String let placeholder: String @Binding var isFocus: Bool func makeNSView(context: Context) -> NSTextField { let tf = NSTextField() tf....

January 29, 2020 · 2 min · 367 words · Khoa

How to observe focus event of NSTextField in macOS

Issue #589 becomeFirstResponder class FocusAwareTextField: NSTextField { var onFocusChange: (Bool) -> Void = { _ in } override func becomeFirstResponder() -> Bool { let textView = window?.fieldEditor(true, for: nil) as? NSTextView textView?.insertionPointColor = R.nsColor.action onFocusChange(true) return super.becomeFirstResponder() } } textField.delegate // NSTextFieldDelegate func controlTextDidEndEditing(_ obj: Notification) { onFocusChange(false) } NSTextField and NSText https://stackoverflow.com/questions/25692122/how-to-detect-when-nstextfield-has-the-focus-or-is-its-content-selected-cocoa When you clicked on search field, search field become first responder once, but NSText will be prepared sometime somewhere later, and the focus will be moved to the NSText....

January 29, 2020 · 1 min · 186 words · Khoa

How to change caret color of NSTextField in macOS

Issue #588 class FocusAwareTextField: NSTextField { var onFocus: () -> Void = {} var onUnfocus: () -> Void = {} override func becomeFirstResponder() -> Bool { onFocus() let textView = window?.fieldEditor(true, for: nil) as? NSTextView textView?.insertionPointColor = R.nsColor.action return super.becomeFirstResponder() } override func resignFirstResponder() -> Bool { onUnfocus() return super.resignFirstResponder() } }

January 29, 2020 · 1 min · 52 words · Khoa

How to make TextView in SwiftUI for macOS

Issue #587 Use NSTextVIew From https://github.com/twostraws/ControlRoom/blob/main/ControlRoom/NSViewWrappers/TextView.swift import SwiftUI /// A wrapper around NSTextView so we can get multiline text editing in SwiftUI. struct TextView: NSViewRepresentable { @Binding private var text: String private let isEditable: Bool init(text: Binding<String>, isEditable: Bool = true) { _text = text self.isEditable = isEditable } init(text: String) { self.init(text: Binding<String>.constant(text), isEditable: false) } func makeNSView(context: Context) -> NSScrollView { let text = NSTextView() text.backgroundColor = isEditable ?...

January 29, 2020 · 2 min · 370 words · Khoa

How to use custom font in SwiftUI

Issue #586 In macOS Add fonts to target. In Info.plist, just need to specify font locations, most of the time they are at Resources folder ATSApplicationFontsPath (String - macOS) identifies the location of a font file or directory of fonts in the bundle’s Resources directory. If present, macOS activates the fonts at the specified path for use by the bundled app. The fonts are activated only for the bundled app and not for the system as a whole....

January 28, 2020 · 2 min · 265 words · Khoa

How to use Firebase Crashlytics in macOS app

Issue #585 New Firebase Crashlytics Follow the new Firebase Crashlytics guide Get started with Firebase Crashlytics using the Firebase Crashlytics SDK CocoaPods Specify FirebaseCore for community managed macOS version of Firebase platform :osx, '10.13' target 'MyMacApp' do # Comment the next line if you don't want to use dynamic frameworks use_frameworks! pod 'FirebaseCore' pod 'Firebase/Crashlytics' end Signing and capabilities Under Hardware runtime, check Disable library validation Under App sandbox, enable Outgoing connections (Client)...

January 28, 2020 · 1 min · 118 words · Khoa