2017-08-03 55 views
1
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控件。NSTextView如何使文本在行的中間

但是我的文本是繪製的,它都是從行底部開始的,這不是我想要的。我希望能夠將文本保持在行的垂直位置。我還沒有找到這方面的案例,請幫我解答。謝謝

enter image description here

回答

0

您可以使用NSAttributedStringKey.baselineOffset(NSBaselineOffsetAttributeName斯威夫特3)調整行中的文本的垂直位置。當然,你需要編寫一些代碼來計算你的行高,並找到正確的基線使用,但此代碼示例應該給你的基本思路:

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

BaselineOffset可以做到這一點,但它將使lineHeightMultiple更難控制,因爲實際高度是lineHeightMultiple + baselineOffset,有沒有其他方便的方法? –

相關問題