Skip to content

Instantly share code, notes, and snippets.

@kirkbyo
Created July 15, 2021 19:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kirkbyo/26be7993baa58f4519d5bdc70e7d85bc to your computer and use it in GitHub Desktop.
Save kirkbyo/26be7993baa58f4519d5bdc70e7d85bc to your computer and use it in GitHub Desktop.
NSTextView AppKit, custom subclass of NSTextBlock bug
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