NSParagraphStyle defaultParagraphStyle not having effect

大兔子大兔子 提交于 2020-01-04 09:29:08

问题


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

来源:https://stackoverflow.com/questions/28473713/nsparagraphstyle-defaultparagraphstyle-not-having-effect

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!