我有一个Attributed字符串,我想降低它的标准行高。要做到这一点,我需要设置负lineSpacing
到我的NSMutableParagraphStyle
。但根据Apple的文档,这是非法的。
有趣的事实是负lineSpacing
实际上工作,但在UILabel
导致额外的底部间距,这取决于线的数量。
是否可以降低线高而不会产生副作用?
使用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))
您也可以从故事板中执行此操作: