我有一个UILabel我想在顶部和底部添加空间的地方.在约束中使用最小高度我将其修改为:

编辑:要做到这一点,我用过:
override func drawTextInRect(rect: CGRect) {
var insets: UIEdgeInsets = UIEdgeInsets(top: 0.0, left: 10.0, bottom: 0.0, right: 10.0)
super.drawTextInRect(UIEdgeInsetsInsetRect(rect, insets))
}
Run Code Online (Sandbox Code Playgroud)
但我要找到一种不同的方法,因为如果我写两行以上的问题是相同的:

我怎样才能在iOS中实现这种风格?

我尝试使用NSAttributedString和设置NSBackgroundColorAttributeName为,blackColor但它没有clearColor行之间的空格.我也试着设置lineSpacing为NSMutableParagraphStyle,但仍然,样式看起来像一个大黑块.
我只需要一个提示,以哪种方式去...谢谢!
换句话说,不是这样的:

默认布局管理器填充没有文本(最后一行除外)的背景颜色(通过 NSAttributedString .backgroundColor 属性指定)。
我已经通过子类化 NSLayoutManager 并覆盖来实现我想要的效果,func drawBackground(forGlyphRange glyphsToShow: NSRange, at origin: CGPoint)如下所示:
override func drawBackground(forGlyphRange glyphsToShow: NSRange, at origin: CGPoint) {
guard let textContainer = textContainers.first, let textStorage = textStorage else { fatalError() }
// This just takes the color of the first character assuming the entire container has the same background color.
// To support ranges of different colours, you'll need to draw each glyph separately, querying the attributed string for the
// background color attribute …Run Code Online (Sandbox Code Playgroud)