Posts

Post not yet marked as solved
0 Replies
1.5k Views
Hi, I need to create a multiline editable text in SwiftUI and also have ability to insert some other text at cursor position. I have created this test application to see how it could work and ended up with this. But it doesn't really work how it should and I am stuck. You should be able to write anything into the main text field and also when you press the button, the text from the textfield should be inserted into the main text at your cursor location. Any help would be much appreciated. // //&#9;InsertTextTestApp.swift //&#9;InsertTextTest // //&#9;Created by Matej Volkmer on 04.02.2021. // import SwiftUI @main struct InsertTextTestApp: App { &#9;&#9;var body: some Scene { &#9;&#9;&#9;&#9;WindowGroup { &#9;&#9;&#9;&#9;&#9;&#9;ContentView() &#9;&#9;&#9;&#9;} &#9;&#9;} } struct ContentView: View { &#9;&#9;@State var text = "Lorem ipsum dolor sit amet." &#9;&#9;@State var insertText = "" &#9;&#9;@State var fieldText = "" &#9;&#9; &#9;&#9;var body: some View { &#9;&#9;&#9;&#9;VStack { &#9;&#9;&#9;&#9;&#9;&#9;CustomTextField(text: $text, insertText: $insertText) &#9;&#9;&#9;&#9;&#9;&#9; &#9;&#9;&#9;&#9;&#9;&#9;TextField("Text to insert", text: $fieldText) &#9;&#9;&#9;&#9;&#9;&#9; &#9;&#9;&#9;&#9;&#9;&#9;Button("Insert") { &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;insertText = fieldText &#9;&#9;&#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;&#9;&#9;.keyboardShortcut(.tab, modifiers: .control) &#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;.frame(width: 400, height: 400) &#9;&#9;} } struct CustomTextField: NSViewRepresentable { &#9;&#9;@Binding var text: String &#9;&#9;@Binding var insertText: String &#9;&#9; &#9;&#9;func makeNSView(context: NSViewRepresentableContext<Self>) -> NSTextView { &#9;&#9;&#9;&#9;let NSView = NSTextView() &#9;&#9;&#9;&#9; &#9;&#9;&#9;&#9;NSView.isEditable = true &#9;&#9;&#9;&#9;NSView.isSelectable = true &#9;&#9;&#9;&#9;NSView.delegate = context.coordinator &#9;&#9;&#9;&#9; &#9;&#9;&#9;&#9;return NSView &#9;&#9;} &#9;&#9;func updateNSView(_ NSView: NSTextView, context: NSViewRepresentableContext<Self>) { &#9;&#9;&#9;&#9;if NSView.string != self.text { &#9;&#9;&#9;&#9;&#9;&#9;NSView.string = self.text &#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9; &#9;&#9;&#9;&#9;NSView.insertText(self.insertText, replacementRange: NSView.selectedRanges.first!.rangeValue) &#9;&#9;} &#9;&#9;func makeCoordinator() -> Coordinator { &#9;&#9;&#9;&#9;return Coordinator(text: $text) &#9;&#9;} &#9;&#9;final class Coordinator : NSObject, NSTextViewDelegate { &#9;&#9;&#9;&#9;var text: Binding<String> &#9;&#9;&#9;&#9; &#9;&#9;&#9;&#9;init(text: Binding<String>) { &#9;&#9;&#9;&#9;&#9;&#9;self.text = text &#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9; &#9;&#9;&#9;&#9;func textViewDidChange(_ NSView: NSTextView) { &#9;&#9;&#9;&#9;&#9; text.wrappedValue = NSView.string &#9;&#9;&#9;&#9;} &#9;&#9;} }
Posted Last updated
.
Post marked as solved
1 Replies
635 Views
Hi, I'm working on an app built entirely with Swift SwiftUI and the new lifecycle. I use Siri Intents and Siri Shortcuts. I have manage to create my own Shortcuts and use them with the Shortcuts app. I can run them manually and the work perfectly. But when I ask Siri to run the Shortcut it doesn't work and Siri just freezes and then cancels the task. Am I missing something out or am I actually doing something totally wrong way? Please let me know. I'm relatively new with Siri Intents so I have followed this tutorial: toolboxpro.app/blog/adding-shortcuts-to-an-app-1
Posted Last updated
.
Post marked as solved
1 Replies
810 Views
Hi, is it possible to create a sidebar in SwiftUI but on the right? Or something like the Xcode inspector area? Thanks very much
Posted Last updated
.
Post marked as solved
1 Replies
389 Views
Hi everyone,Is there a way to get a number of all notification in iPhone / iPad (from notification center)? Or just number of badges in apps?Tried tu use UNUserNotificationCenter, but I wasnt able to make it work...Thanks for answer,Matej
Posted Last updated
.