I have introduced a styled NSViewRepresentable text field on to my layout in order to use an attributed Text Editor field. I want to influence the content of this text field through some buttons.
I can change the styled attributes of the field, but it never refreshes unless I hit it manually. There must be a way of getting to do this automatically. What have I missed?
import SwiftUI
import Combine
let publisher = PassthroughSubject<Bool,Never>()
struct ContentView: View
{
@State var mytext = NSMutableAttributedString(string: "")
var attributedString: NSMutableAttributedString
@State var textViewID: Int = 0
init()
{
attributedString = NSMutableAttributedString(
string: "Hello World!",
attributes: [.font: NSFont.systemFont(ofSize: 18)])
_mytext = State(initialValue: attributedString)
}
var body: some View
{
VStack
{
Button("Bold")
{
attributedString.addAttribute(.font, value: NSFont.boldSystemFont(ofSize: 18), range: NSRange(location: 0, length: 5))
mytext = attributedString
publisher.send(true)
}
}
TextView(text: $mytext)
.frame(maxWidth: .infinity, maxHeight: .infinity)
.id(textViewID)
.onReceive(publisher) { ( stateTo ) in
textViewID += 1
}
}
}
struct TextView: NSViewRepresentable
{
@Binding var text: NSMutableAttributedString
func makeNSView(context: Context) -> NSTextView
{
NSTextView()
}
func updateNSView(_ nsView: NSTextView, context: Context)
{
nsView.textStorage?.setAttributedString(text)
}
}
Or am I missing something and there is a much easier way?