如何减少行间距?

问题描述 投票:-2回答:1

我有一个Attributed字符串,我想降低它的标准行高。要做到这一点,我需要设置负lineSpacing到我的NSMutableParagraphStyle。但根据Apple的文档,这是非法的。

有趣的事实是负lineSpacing实际上工作,但在UILabel导致额外的底部间距,这取决于线的数量。

enter image description here

是否可以降低线高而不会产生副作用?

ios swift nsattributedstring
1个回答
1
投票

使用NSParagraphStyle.lineHightMultiple https://developer.apple.com/documentation/uikit/nsparagraphstyle/1528614-lineheightmultiple

您可以将lineHeightMultiple设置为大于0但小于1的值,它将减少行间距。

let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.lineHeightMultiple = 0.83 //Try different values here to see what looks best

let attrString = NSMutableAttributedString(string: "Your string")
attrString.addAttribute(.paragraphStyle, value: paragraphStyle, range: NSMakeRange(0, attrString.length))

您也可以从故事板中执行此操作:

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.