可滚动 NSTextView 与自定义 NSTextStorage 进行格式化
我正在尝试制作一个适用于 Mac 操作系统的文本编辑器。我将 NSTextView 与自定义 NSTextStorage 类一起使用。它将诸如粗体等属性应用于NSAttributableStrings
。
这一切似乎工作正常,如下面的屏幕截图之一所示。这是一个带有自定义 NSTextStorage
类的 NSTextView
。它通过 NSAttributeableString
但是,一切都相同,但从 Apple 提供的函数获取可滚动的 NSTextView NSTextView.scrollableTextView()
它根本不显示任何文本。即使您可以在屏幕截图中看到 NStextView
实际上是可见的。此外,将鼠标移到编辑器上会将光标更改为编辑器光标。但我无法选择、输入或执行任何操作。
执行与上面完全相同的操作,但不向文本视图提供文本容器确实表明它已正确连接,因为我确实获得了可滚动的文本视图 然后。滚动实际上起作用的地方,但格式当然不再应用。
所以我对现在必须做什么感到困惑。
这基本上是我的设置:
//
// TextDocumentViewController.swift
//
// Created by Matthijn on 15/02/2022.
// Based on LayoutWithTextKit2 Sample from Apple
import Foundation
import AppKit
class TextDocumentViewController: NSViewController {
// Extends NSTextStorage, applies attributes to NSAttributeAbleString for formatting
private var textStorage: TextStorage
// Not custom yet, default implementation - do I need to subclass this specifically and implement something to support the scrolling behaviour? Which seems weird to me since it does work without scrolling support
private var layoutManager: NSLayoutManager
// Also default implementation
private var textContainer: NSTextContainer
private var textDocumentView: NSTextView
private var scrollView: NSScrollView
required init(content: String) {
textStorage = TextStorage(editorAttributes: MarkdownAttributes)
layoutManager = NSLayoutManager()
textStorage.addLayoutManager(layoutManager)
textContainer = NSTextContainer()
// I'm not 100% sure if I need this on false or true or can just do defaults. No combination fixes it
// textContainer.heightTracksTextView = false
// textContainer.widthTracksTextView = true
layoutManager.addTextContainer(textContainer)
scrollView = NSTextView.scrollableTextView()
textDocumentView = (scrollView.documentView as! NSTextView)
// Basically commenting this out, stops applying my NSTextContainer, NSLayoutManager and NSTextContainer, but then of course the formatting is not applied. This one line changes it between it works without formatting, or it doesn't work at all. (Unless I have my text view not embedded in a scroll view) then it works but the scrolling of course then does not work.
textDocumentView.textContainer = textContainer
textDocumentView.string = content
textDocumentView.isVerticallyResizable = true
textDocumentView.isHorizontallyResizable = false
super.init(nibName: nil, bundle: nil)
}
override func loadView() {
view = scrollView
}
}
I'm trying to make a text editor with formatting for Mac OS. Which I have working using an NSTextView
together with a custom NSTextStorage
class. Which applies attributes like bold etc to NSAttributableStrings
.
This all seems to work fine as seen in screenshot one below. Which is an NSTextView
with a custom NSTextStorage
class attached to it. Which applies the formatting through attributes on an NSAttributeableString
However, having everything the same, but getting a scrollable NSTextView from the Apple supplied function NSTextView.scrollableTextView()
it does not display any text at all. Even though you can see in the screenshot that the NStextView
is actually visible. Also, moving my mouse over the editor changes the cursor to the editor cursor. But I can't select, type or do anything.
Doing the exact same thing as above, but not supplying a text container to the text view does show that it is wired up correctly, since I do get a scrollable text view then. Where the scrolling actually works, but then of course the formatting is no longer applied.
So I'm confused on what I have to do now.
This is basically my setup:
//
// TextDocumentViewController.swift
//
// Created by Matthijn on 15/02/2022.
// Based on LayoutWithTextKit2 Sample from Apple
import Foundation
import AppKit
class TextDocumentViewController: NSViewController {
// Extends NSTextStorage, applies attributes to NSAttributeAbleString for formatting
private var textStorage: TextStorage
// Not custom yet, default implementation - do I need to subclass this specifically and implement something to support the scrolling behaviour? Which seems weird to me since it does work without scrolling support
private var layoutManager: NSLayoutManager
// Also default implementation
private var textContainer: NSTextContainer
private var textDocumentView: NSTextView
private var scrollView: NSScrollView
required init(content: String) {
textStorage = TextStorage(editorAttributes: MarkdownAttributes)
layoutManager = NSLayoutManager()
textStorage.addLayoutManager(layoutManager)
textContainer = NSTextContainer()
// I'm not 100% sure if I need this on false or true or can just do defaults. No combination fixes it
// textContainer.heightTracksTextView = false
// textContainer.widthTracksTextView = true
layoutManager.addTextContainer(textContainer)
scrollView = NSTextView.scrollableTextView()
textDocumentView = (scrollView.documentView as! NSTextView)
// Basically commenting this out, stops applying my NSTextContainer, NSLayoutManager and NSTextContainer, but then of course the formatting is not applied. This one line changes it between it works without formatting, or it doesn't work at all. (Unless I have my text view not embedded in a scroll view) then it works but the scrolling of course then does not work.
textDocumentView.textContainer = textContainer
textDocumentView.string = content
textDocumentView.isVerticallyResizable = true
textDocumentView.isHorizontallyResizable = false
super.init(nibName: nil, bundle: nil)
}
override func loadView() {
view = scrollView
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您实际上可以使用
scrollableTextView()
创建NSScrollView
实例,并且可以获得隐式创建的documentView (NSTextView)
。最后,可以将
documentView
的现有LayoutManager
分配给继承自NSTextStorage
的自己的TextStorage
类。在
viewDidLoad
中,您可以使用addSubview:
以传统方式将scrollView
添加到视图中。对于 AutoLayout,一如既往,translatesAutoresizingMaskIntoConstraints
必须设置为false
。使用
widthAnchor
和greaterThanOrEqualToConstant
您可以定义最小尺寸,因此用户无法将其周围的窗口变小。该结构还允许以后使用附加的粘性视图(例如面包屑视图等)进行潜在的简单扩展。代码
如果您以这种方式实现它,那么对于一个小型的最小测试,它可能看起来像这样。
测试
以下是一个快速测试,显示显示和滚动都按设置的预期工作:
You can actually create the
NSScrollView
instance withscrollableTextView()
and you can get the implicitly createddocumentView (NSTextView)
.Finally, one can assign the existing
LayoutManager
of thedocumentView
to the ownTextStorage
class inheriting fromNSTextStorage
.In
viewDidLoad
you could then add thescrollView
traditionally to the view usingaddSubview:
. For AutoLayout, as always,translatesAutoresizingMaskIntoConstraints
must be set tofalse
.With
widthAnchor
and agreaterThanOrEqualToConstant
you can define a minimum size, so the window around it cannot be made smaller by the user. The structure also allows potential later simple extensions with additional sticky views (e.g. breadcrumb view etc).Code
If you implement it this way, then for a small minimal test it might look something like this.
Test
Here's a quick test showing that both display and scrolling work as expected with the setup: