Created
July 15, 2021 19:53
-
-
Save kirkbyo/26be7993baa58f4519d5bdc70e7d85bc to your computer and use it in GitHub Desktop.
NSTextView AppKit, custom subclass of NSTextBlock bug
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import SwiftUI | |
struct ContentView: View { | |
var body: some View { | |
NativeEditor() | |
.frame(maxWidth: 400, maxHeight: 600) | |
} | |
} | |
struct NativeEditor: NSViewRepresentable { | |
typealias NSViewType = NSTextView | |
func makeNSView(context: Context) -> NSTextView { | |
let textView = NSTextView() | |
textView.textStorage?.append(NSAttributedString(string: "\n")) | |
textView.textStorage?.append(NSAttributedString(string: "\n")) | |
textView.textStorage?.append(NSAttributedString(string: "\n")) | |
let style1 = NSMutableParagraphStyle() | |
style1.textBlocks = [CodeBlock()] | |
textView.textStorage?.append(NSAttributedString(string: "Hello world 1\n", attributes: [ | |
NSAttributedString.Key.paragraphStyle: style1 | |
])) | |
textView.textStorage?.append(NSAttributedString(string: "\n")) | |
textView.textStorage?.append(NSAttributedString(string: "\n")) | |
textView.textStorage?.append(NSAttributedString(string: "\n")) | |
return textView | |
} | |
func updateNSView(_ nsView: NSTextView, context: Context) {} | |
} | |
class CodeBlock: NSTextBlock { | |
override init() { | |
super.init() | |
setValue(100, type: .percentageValueType, for: .width) | |
backgroundColor = NSColor(white: 0.6, alpha: 1.0) | |
} | |
required init?(coder: NSCoder) { | |
fatalError("init(coder:) has not been implemented") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment