如何设置 NSTextView行高 ,需要用到NSLayoutManagerDelegate
view.layoutManager?.delegate = self
添加属性设置行高的数值。
let lineHeightMultiple: CGFloat = 1.6
let font: NSFont = NSFont.systemFont(ofSize: NSFont.systemFontSize)
在代理方写入如下
func layoutManager(_ layoutManager: NSLayoutManager, shouldSetLineFragmentRect lineFragmentRect: UnsafeMutablePointer<NSRect>, lineFragmentUsedRect: UnsafeMutablePointer<NSRect>, baselineOffset: UnsafeMutablePointer<CGFloat>, in textContainer: NSTextContainer, forGlyphRange glyphRange: NSRange) -> Bool {
let fontLineHeight = layoutManager.defaultLineHeight(for: font)
let lineHeight = fontLineHeight * lineHeightMultiple
let baselineNudge = (lineHeight - fontLineHeight)
// The following factor is a result of experimentation:
* 0.6
var rect = lineFragmentRect.pointee
rect.size.height = lineHeight
var usedRect = lineFragmentUsedRect.pointee
usedRect.size.height = max(lineHeight, usedRect.size.height) // keep emoji sizes
lineFragmentRect.pointee = rect
lineFragmentUsedRect.pointee = usedRect
baselineOffset.pointee = baselineOffset.pointee + baselineNudge
return true
}