Skip to content

Instantly share code, notes, and snippets.

@drypot
Created December 24, 2024 01:44
Show Gist options
  • Select an option

  • Save drypot/33bd5e5dbf87cba379eaf248d09499b4 to your computer and use it in GitHub Desktop.

Select an option

Save drypot/33bd5e5dbf87cba379eaf248d09499b4 to your computer and use it in GitHub Desktop.
//
// NSViewControllerRepresentable.swift
// HelloSwiftUI
//
// Created by Kyuhyun Park on 12/23/24.
//
import SwiftUI
import AppKit
fileprivate struct Representable: NSViewControllerRepresentable {
@Binding var text: String
func makeCoordinator() -> Coordinator {
// Coordinator(text: $text)
Coordinator()
}
func makeNSViewController(context: Context) -> Controller {
Controller(text: $text)
}
func updateNSViewController(_ controller: Controller, context: Context) {
controller.textView.string = text
}
class Controller: NSViewController, NSTextViewDelegate {
var textView: NSTextView!
@Binding var text: String
init(text: Binding<String>) {
self._text = text
super.init(nibName: nil, bundle: nil)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func loadView() {
let container = NSView()
let textView = NSTextView()
textView.translatesAutoresizingMaskIntoConstraints = false
textView.delegate = self
self.textView = textView
container.addSubview(textView)
NSLayoutConstraint.activate([
textView.leadingAnchor.constraint(equalTo: container.leadingAnchor),
textView.trailingAnchor.constraint(equalTo: container.trailingAnchor),
textView.topAnchor.constraint(equalTo: container.topAnchor),
textView.bottomAnchor.constraint(equalTo: container.bottomAnchor)
])
self.view = container
}
func textDidChange(_ notification: Notification) {
self.text = textView.string
}
// AppKit 데이터를 SwiftUI 데이터로.
func textChanged(text: String) {
}
}
@MainActor
class Coordinator: NSObject /*, MyDelegate*/ {
// @Binding var text: String
//
// init(text: Binding<String>) {
// _text = text
// }
// // AppKit 데이터를 SwiftUI 데이터로.
// func textChanged(text: String) {
// self.text = text
// }
}
}
struct NSViewControllerRepresentableDemo: View {
@State private var text:String = "Hello"
var body: some View {
VStack {
Text("NSViewControllerRepresentable Demo")
.font(.title)
.padding()
Text(text)
.font(.title3)
.padding()
Representable(text: $text)
.frame(width: 200, height: 80)
}
.padding()
}
}
#Preview {
NSViewControllerRepresentableDemo()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment