【问题标题】:(AppKit) Tab insertion inside of NSTextBlock(AppKit) 在 NSTextBlock 内插入制表符
【发布时间】: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")
}}

我尝试过的

  1. 在初始化ParagraphStyle 类的paragraphStyle 属性值时,将NSTextTabs 添加到.tabStops 数组中。
  2. 通过addTabStop 方法添加NSTextTab。
  3. 将值设置为defaultTabInterval
  4. 将 textView 的 nextKeyViewnextResponder 设置为零。

剩下的唯一一件事(我能想到的)就是在 textview 是第一响应者时监听用户按下 tab 键的情况。当事件发生时,我将以编程方式在用户的插入符号所在的位置插入 Tab 空白,然后以编程方式将插入符号移回它应该在的位置。然而,这是解决这个看似简单的问题的一种非常迂回的方法,这就是为什么我相信我可能遗漏了一些明显的东西。希望这里有经验的开发者有其他建议!

提前谢谢你!

【问题讨论】:

    标签: swift appkit nstextview textkit


    【解决方案1】:

    显然,文本块中的选项卡会将插入点移动到下一个文本块,这在表格中很有意义。解决方法:子类NSTextView,覆盖insertTab 并插入一个选项卡。

    override func insertTab(_ sender: Any?) {
        insertText("\t", replacementRange: selectedRange())
    }
    

    【讨论】:

    • 非常感谢!这似乎奏效了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-08-08
    • 1970-01-01
    • 1970-01-01
    • 2017-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多