【问题标题】:How to Set the Paragraph Property Width of NSTextView如何设置 NSTextView 的段落属性宽度
【发布时间】:2019-02-20 17:45:05
【问题描述】:

我的英文很烂,不知道怎么形容。

请看我先上传的图片。这是 Ulysses 的截图。

这应该是一个 NSTextView 控件。选中所有文本后,我发现它可以控制特定段落的左偏移。

我试着模仿它。

但是我发现NSMutable Paragraph Style好像没有达到这样的效果,只能达到类似下图的效果。

简单的说,我想实现上一张图的效果,却只有第二张图的效果。

我看了另一个问题,不过主要是关于背景颜色的,想知道缩进的问题,这样可以得到更多的结果。

NSTextView selection highlights all characters even paragraph indents

Ulysses 是如何实现的?

【问题讨论】:

标签: cocoa nstextview nsparagraphstyle


【解决方案1】:

您可以通过在 NSLayoutManager 子类中覆盖 fillBackgroundRectArray(_: count:forCharacterRange:color:) 来修改选择的绘制矩形。

class MyLayoutManager: NSLayoutManager {

    override func fillBackgroundRectArray(_ rectArray: UnsafePointer<NSRect>, count rectCount: Int, forCharacterRange charRange: NSRange, color: NSColor) {
        // selection rects to draw a highlight.
        var rects: [NSRect] = Array(UnsafeBufferPointer(start: rectArray, count: rectCount))

        // modify rects here...

        // call super and pass in your custom rects array.
        super.fillBackgroundRectArray(&rects, count: rectCount, forCharacterRange: charRange, color: color)
    }

}

你可以实现这样的目标:


顺便说一句,您可以使用以下方法替换文本视图的布局管理器:

textView.textContainer?.replaceLayoutManager(MyLayoutManager())

【讨论】:

    猜你喜欢
    • 2013-06-15
    • 1970-01-01
    • 2013-07-22
    • 2021-10-01
    • 2017-11-03
    • 1970-01-01
    • 1970-01-01
    • 2014-10-07
    • 2012-02-28
    相关资源
    最近更新 更多