I would say that you need to use PDFKit view object inside a SwiftUI component like I did to use a TextView in ly SwiftUI appThis is a simplified version : import SwiftUI
import os
let uiLog = OSLog(subsystem: "com.visual-science.CryptiK", category: "UI")
class EditorCoordinator : NSObject, NSTextViewDelegate {
let textView: NSTextView;
let scrollView : NSScrollView
let text : Binding<NSAttributedString>
init(binding: Binding<NSAttributedString>) {
text = binding
textView = NSTextView(frame: .zero)
textView.autoresizingMask = [.height, .width]
textView.textStorage?.setAttributedString(text.wrappedValue)
textView.textColor = NSColor.textColor
scrollView = NSScrollView(frame: .zero)
scrollView.hasVerticalScroller = true
scrollView.autohidesScrollers = false
scrollView.autoresizingMask = [.height, .width]
scrollView.documentView = textView
super.init()
textView.delegate = self
}
func textDidChange(_ notification: Notification) {
switch notification.name {
case NSText.didChangeNotification :
text.wrappedValue = (notification.object as? NSTextView)?.textStorage ?? NSAttributedString(string: "")
default:
os_log(.error, log: uiLog, "Coordinator received unwanted notification")
}
}
}
struct DataTextEditorView: View, NSViewRepresentable {
typealias Coordinator = EditorCoordinator
typealias NSViewType = NSScrollView
let text : Binding<NSAttributedString>
func makeNSView(context: NSViewRepresentableContext<DataTextEditorView>) -> DataTextEditorView.NSViewType {
os_log(.info, log: uiLog, "%@", context.coordinator.scrollView)
return context.coordinator.scrollView
}
func updateNSView(_ nsView: NSScrollView, context: NSViewRepresentableContext<DataTextEditorView>) {
os_log(.debug, log: uiLog, "%@", context.coordinator.self)
os_log(.debug, log: uiLog, "%@", text.wrappedValue)
}
func makeCoordinator() -> EditorCoordinator {
os_log(.info, log: uiLog, "makeCoordinator")
let coordinator = EditorCoordinator(binding: text)
return coordinator
}
}This is the kind of thing you need to do to use UIKit/AppKit UI element in a SwiftUI based AppRegardsGerard
Post
Replies
Boosts
Views
Activity
Well my own answer, can be useful for others.I hesitated to use my own button style ... but this is the solution here in my casestruct SimpleButtonStyle: ButtonStyle {
func pressColor(isPressed: Bool) -> Color{
if isPressed {
return Color.gray
}
else {
return Color.white
}
}
func makeBody(configuration: Self.Configuration) -> some View {
configuration.label
.padding([.trailing, .leading], 10)
.padding([.top, .bottom], 1)
.background(
RoundedRectangle(cornerRadius: 5)
.fill(pressColor(isPressed: configuration.isPressed))
.overlay(RoundedRectangle(cornerRadius: 5)
.stroke(lineWidth: 1)
.foregroundColor(Color.gray)
)
)
}
}
struct SomeButton : View {
var body: some View {
HStack{
Button(action: {
print("Pressed")
}) {
Text("Press")
}
}
.buttonStyle(SimpleButtonStyle())
.font(.headline)
.padding(.trailing, 10)
}
}Take care