Skip to content

Instantly share code, notes, and snippets.

@kowei
kowei / ContentView.swift
Last active April 1, 2021 07:31
default ContentView.swift
import SwiftUI
struct ContentView: View {
var body: some View {
Text("Hello, world!")
.padding()
}
}
struct ContentView_Previews: PreviewProvider {
@kowei
kowei / SwiftUI.swift
Created March 30, 2021 03:57
Text init definition in SwiftUI
@available(iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 6.0, *)
extension Text {
/// Creates a text view that displays localized content identified by a key.
///
/// Use this initializer to look for the `key` parameter in a localization
/// table and display the associated string value in the initialized text
/// view. If the initializer can't find the key in the table, or if no table
/// exists, the text view displays the string representation of the key
/// instead.
@kowei
kowei / ContentView.swift
Last active April 1, 2021 07:31
ContentView in multi-language
import SwiftUI
struct ContentView: View {
var body: some View {
Text("Hello, world!")
.padding()
}
}
struct ContentView_Previews: PreviewProvider {
@kowei
kowei / LocaleDemoApp.swift
Created April 1, 2021 07:30
make app's variable lnag as state-binding, and send to environment
struct LocalDemoApp: App {
@State(initialValue: "en") var lang: String
var body: some Scene {
WindowGroup {
ContentView(lang: $lang)
.environment(\.locale, .init(identifier: lang))
}
}
}
@kowei
kowei / ContentView.swift
Created April 1, 2021 07:38
ContentView.swift with lang binding from app entry and 2 buttons for changing lang value
import SwiftUI
struct ContentView: View {
@Binding var lang: String
var body: some View {
VStack(alignment: .center, spacing: 30){
Text("Hello, world!")
.padding()
Button("English") {
@kowei
kowei / ContentView.swift
Last active April 1, 2021 08:46
preview with wrapper for sending binding value
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
VStack{
Text("Preview in English").foregroundColor(.green)
PreviewWrapper(lang: .init(initialValue: "en"))
}
VStack{
Text("Preview in Spanish").foregroundColor(.orange)
PreviewWrapper(lang: .init(initialValue: "es"))
}
import Foundation
import XcodeKit
class BetterCommand: NSObject, XCSourceEditorCommand {
func perform(with invocation: XCSourceEditorCommandInvocation, completionHandler: @escaping (Error?) -> Void ) -> Void {
print("BetterCommand: \(invocation.description)")
print("BetterCommand: \(invocation.commandIdentifier)")
completionHandler(nil)
}
@kowei
kowei / Info.swift
Last active April 16, 2021 05:19
Extension Info.plist
<key>NSExtension</key>
<dict>
<key>NSExtensionAttributes</key>
<dict>
<key>XCSourceEditorCommandDefinitions</key>
<array>
<dict>
<key>XCSourceEditorCommandClassName</key>
<string>$(PRODUCT_MODULE_NAME).BetterCommand</string>
<key>XCSourceEditorCommandIdentifier</key>
<key>NSExtension</key>
<dict>
<key>NSExtensionAttributes</key>
<dict>
<key>XCSourceEditorCommandDefinitions</key>
<array>
<dict>
<key>XCSourceEditorCommandClassName</key>
<string>$(PRODUCT_MODULE_NAME).BetterCommand</string>
<key>XCSourceEditorCommandIdentifier</key>
import Foundation
import XcodeKit
class BetterCommand: NSObject, XCSourceEditorCommand {
func perform(with invocation: XCSourceEditorCommandInvocation, completionHandler: @escaping (Error?) -> Void) {
switch invocation.commandIdentifier {
case "BetterHelloWorld":
insertHelloWorl(source: invocation.buffer)
completionHandler(nil)
return