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

这是我所有的代码,在main.storyboard中,只有一个NSTextView控件。
但我的文字是画出来的,都是从最下面一行开始的,这不是我想要的。我希望能够保持文本在行的垂直位置。我在这个地区还没有找到病例,请帮我回答。谢谢您
swift - NSTextView如何使文本在行的中间-LMLPHP

最佳答案

可以使用NSAttributedStringKey.baselineOffset(Swift 3中的NSBaselineOffsetAttributeName)调整文本在行内的垂直位置。当然,您需要编写一些代码来计算行高并找到要使用的正确基线,但此代码示例应为您提供基本思想:

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

关于swift - NSTextView如何使文本在行的中间,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45491818/

10-12 00:41