【发布时间】:2016-11-17 14:03:32
【问题描述】:
当我使用 Text Kit 绘制具有固定行高的属性字符串时,字符总是与行片段的底部对齐。虽然这在字符大小不同的一行上是有意义的,但这会破坏多行文本的流动。基线的出现由每行的最大下降点决定。
我从 Sketch 背后的人那里找到了 article,更详细地解释了这个确切的问题并展示了他们的解决方案的作用,但显然没有解释 如何他们实现了这一目标。 p>
我正在使用的代码:
let smallFont = UIFont.systemFont(ofSize: 15)
let bigFont = UIFont.systemFont(ofSize: 25)
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.minimumLineHeight = 22
paragraphStyle.maximumLineHeight = 22
var attributes = [
NSFontAttributeName: smallFont,
NSParagraphStyleAttributeName: paragraphStyle
]
let textStorage = NSTextStorage()
let textContainer = NSTextContainer(size: CGSize(width: 250, height: 500))
let layoutManager = NSLayoutManager()
textStorage.append(NSAttributedString(string: "It is a long established fact that a reader will be ", attributes:attributes))
attributes[NSFontAttributeName] = bigFont
textStorage.append(NSAttributedString(string: "distracted", attributes:attributes))
attributes[NSFontAttributeName] = smallFont
textStorage.append(NSAttributedString(string: " by the readable content of a page when looking at its layout.", attributes:attributes))
layoutManager.addTextContainer(textContainer)
textStorage.addLayoutManager(layoutManager)
let textView = UITextView(frame: self.view.bounds, textContainer:textContainer)
view.addSubview(textView)
【问题讨论】: