简体   繁体   English

NSTextView如何使文本在行的中间

[英]How does the NSTextView make the text in the middle of the line

class ViewController: NSViewController {
    @IBOutlet var editView: NSTextView!
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        let
        paragraphStyle = NSParagraphStyle.default().mutableCopy() as! NSMutableParagraphStyle
        paragraphStyle.lineHeightMultiple = 1.6
        editView.defaultParagraphStyle = paragraphStyle;
        //editView.typingAttributes[NSFontAttributeName] = paragraphStyle;
        editView.typingAttributes[NSParagraphStyleAttributeName] = paragraphStyle;

    }

    override var representedObject: Any? {
        didSet {
        // Update the view, if already loaded.
        }
    }
}

This is all my code, in main.storyboard, there's only one NSTextView control. 这就是我所有的代码,在main.storyboard中,只有一个NSTextView控件。

But my text is drawn, and it's all started at the bottom of the row, which is not what I want. 但是我的文字是绘制的,并且所有内容都从该行的底部开始,这不是我想要的。 I want to be able to keep the text in the vertical position of the row. 我希望能够将文本保持在行的垂直位置。 I have not found a case in this area, please help me answer. 我在这方面没有找到案例,请帮助我回答。 thank you 谢谢

在此处输入图片说明

You can use NSAttributedStringKey.baselineOffset (NSBaselineOffsetAttributeName in Swift 3) to adjust the vertical position of the text within the line. 您可以使用NSAttributedStringKey.baselineOffset(在Swift 3中为NSBaselineOffsetAttributeName)来调整文本在行中的垂直位置。 You will of course need to write a little code to calculate your line height and find the correct baseline to use, but this code sample should give you the basic idea: 当然,您将需要编写一些代码来计算行高并找到要使用的正确基线,但是此代码示例应为您提供基本概念:

class ViewController: NSViewController {
    @IBOutlet var editView: NSTextView!
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        let paragraphStyle = NSParagraphStyle.default.mutableCopy() as! NSMutableParagraphStyle
        paragraphStyle.lineHeightMultiple = 1.6
        editView.defaultParagraphStyle = paragraphStyle
        editView.typingAttributes[.paragraphStyle] = paragraphStyle
        editView.typingAttributes[.baselineOffset] = 15
    }

    override var representedObject: Any? {
        didSet {
            // Update the view, if already loaded.
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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