【发布时间】:2021-08-13 21:55:46
【问题描述】:
我正在使用 AppKit 为 macOS 开发 Markdown 编辑器,并且已经掌握了所有基础知识并可以正常工作。我使用NSTextBlock 作为代码块。
然而,我遇到的一个问题是,在 NSTextBlock 中键入 Tab 会导致插入符号向下移动到下一段,而不是插入制表符空白。期望的结果是用户能够在 NSTextBlock 内插入选项卡。
我创建了一个示例项目来演示 GitHub 上的这种行为。
快速浏览示例项目:
这是一个简单的 AppKit 应用程序。我在 Main.storyboard 中的默认 ViewController 中添加了一个可编辑、可滚动的文本视图。 textView 通过 IBOutlet 连接到 ViewController 类。
在自动生成的 viewDidLoad() 方法中,我调用了 insertText 函数,我编写该函数的目的是在 NSTextView 中插入一行文本,然后是两个带有 NSTextBlocks 样式的段落。
func insertText(textStorage: NSTextStorage) {
//Insert first line of regular text
var attributes: [NSAttributedString.Key: Any] = [
.font: NSFont.systemFont(ofSize: 24),
.foregroundColor: NSColor.textColor,
.backgroundColor: NSColor.clear
]
let attributedString1: NSAttributedString = NSAttributedString(string: "Test\n", attributes: attributes)
textStorage.append(attributedString1)
//Insert the blue block holdig text
attributes[.foregroundColor] = NSColor.white
attributes[.paragraphStyle] = ParagraphStyle(bgColor: NSColor.systemBlue).paragraphStyle
let attributedBlock1 = NSAttributedString(string: "Hello, I'm the blue block\n", attributes: attributes)
textStorage.append(attributedBlock1)
//Insert the pink block holdig text
attributes[.foregroundColor] = NSColor.black
attributes[.paragraphStyle] = ParagraphStyle(bgColor: NSColor.systemPink).paragraphStyle
let attributedBlock2 = NSAttributedString(string: "Hello, I'm the pink block\n", attributes: attributes)
textStorage.append(attributedBlock2)
}
这样的效果:
如果您启动应用程序并尝试在插入符号位于第一段开头时插入 Tab,则 Tab 空格会按预期添加到该段落的开头。但是,如果您尝试对使用 NSTextBlocks 样式的段落执行相同操作,插入符号只会向下移动到下一个段落,并且不会插入制表符空白。
我为示例项目编写的另外两个类是 ParagraphStyle 和 CustomTextBlock。
对于ParagraphStyle 类,保留用于蓝色和粉色块属性的段落样式:
class ParagraphStyle {
let bgColor: NSColor
let paragraphStyle: NSParagraphStyle
init(bgColor: NSColor) {
self.bgColor = bgColor
//Set paragraph style
self.paragraphStyle = {
let mutableParagraphStyle = NSMutableParagraphStyle()
let specialBlock = CustomTextBlock(bgColor: bgColor)
mutableParagraphStyle.textBlocks.append(specialBlock)
let style = mutableParagraphStyle as NSParagraphStyle
return style
}()
}}
对于CustomTextBlock类,继承自NSTextBlock,我定义了块的颜色和尺寸:
class CustomTextBlock: NSTextBlock {
init(bgColor: NSColor) {
super.init()
//Control the BG color of the text block
self.backgroundColor = bgColor
//Control dimensions of the text block
self.setValue(100, type: NSTextBlock.ValueType.percentageValueType, for: NSTextBlock.Dimension.width)
self.setValue(50, type: NSTextBlock.ValueType.absoluteValueType, for: NSTextBlock.Dimension.minimumHeight)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}}
我尝试过的
- 在初始化
ParagraphStyle类的paragraphStyle 属性值时,将NSTextTabs 添加到.tabStops 数组中。 - 通过addTabStop 方法添加NSTextTab。
- 将值设置为defaultTabInterval
- 将 textView 的
nextKeyView和nextResponder设置为零。
剩下的唯一一件事(我能想到的)就是在 textview 是第一响应者时监听用户按下 tab 键的情况。当事件发生时,我将以编程方式在用户的插入符号所在的位置插入 Tab 空白,然后以编程方式将插入符号移回它应该在的位置。然而,这是解决这个看似简单的问题的一种非常迂回的方法,这就是为什么我相信我可能遗漏了一些明显的东西。希望这里有经验的开发者有其他建议!
提前谢谢你!
【问题讨论】:
标签: swift appkit nstextview textkit