简体   繁体   中英

How to Set the Paragraph Property Width of NSTextView

My English is terrible, and I don't know how to describe it.

Please look at the picture I uploaded first. This is a screenshot from Ulysses.

在此处输入图片说明

This should be a NSTextView control. After selecting all the text, I found that it could control the left-hand offset of a particular paragraph.

I tried to imitate it.

But I found that NSMutable Paragraph Style does not seem to achieve such an effect, it can only achieve the effect similar to that of the following picture.

在此处输入图片说明

Simply put, I want to achieve the effect of the previous picture, but only the effect of the second picture.

I looked at another question, but it was mainly about background color, and I wanted to know the question of indentation so that I could achieve more results.

NSTextView selection highlights all characters even paragraph indents

How is Ulysses implemented?

You can modify drawing rectangles of selection by overriding fillBackgroundRectArray(_: count:forCharacterRange:color:) in your NSLayoutManager subclass.

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)
    }

}

And you can achieve something like this:

在此处输入图片说明


BTW, you can replace text view's layout manager by using:

textView.textContainer?.replaceLayoutManager(MyLayoutManager())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM