NSParagraphStyle defaultParagraphStyle not having

2019-07-21 00:04发布

问题:

Trying to set the paragraph styles for an NSTextView. Am I doing something wrong here, or putting the code in the wrong place perhaps..?

I have this code in the subclass of my NSTextView.

This does not affect my text view in any way:

class EditorTextView: NSTextView {

    override func drawRect(dirtyRect: NSRect) {
        super.drawRect(dirtyRect)
        // to do
    }

    override func awakeFromNib() {
        var parastyle:NSMutableParagraphStyle =  NSMutableParagraphStyle()
        parastyle.lineSpacing = 20
        self.defaultParagraphStyle = parastyle
    }

}

Expected result: When I start typing into the text view, these styles should have been implemented, unless manually overridden by the user via in app formatting menu items.

Edit: Oh I see. I must apply the styles to the attributed string in text storage, right after the above :

        var storagerange = self.attributedString().length
        self.textStorage?.addAttribute(NSParagraphStyleAttributeName, value: parastyle, range: NSMakeRange(0, storagerange))