快速更改行距可保留底部空白

时间:2019-06-18 09:39:00

标签: ios swift iphone xcode ipad

我正在尝试更改标签的行距,以减少阿拉伯语的行距,这实在太多了。我在here中使用的扩展功能加上阿拉伯样式的附加功能正在控制标签的行距,但是唯一的问题是我认为在减小行距之前,它的下空白留白了,等于空白。 / p>

此处的扩展功能:

extension UILabel {
// Pass value for any one of both parameters and see result
func setLineSpacing(lineSpacing: CGFloat = 0.0, lineHeightMultiple: CGFloat = 0.0) {

    guard let labelText = self.text else { return }

    let paragraphStyle = NSMutableParagraphStyle()
    paragraphStyle.lineSpacing = lineSpacing
    paragraphStyle.lineHeightMultiple = lineHeightMultiple

    paragraphStyle.alignment = .justified
    paragraphStyle.baseWritingDirection = .rightToLeft

    let attributedString:NSMutableAttributedString

    if let labelattributedText = self.attributedText {
        attributedString = NSMutableAttributedString(attributedString: labelattributedText)
    } else {
        attributedString = NSMutableAttributedString(string: labelText)
    }

    attributedString.addAttribute(NSAttributedString.Key.paragraphStyle, value:paragraphStyle, range:NSMakeRange(0, attributedString.length))

    self.attributedText = attributedString
}

}

然后我就这样调用函数:

bodyLabel.attributedText = entry.attributedText
bodyLabel.setLineSpacing(lineSpacing: -20)

enter image description here

2 个答案:

答案 0 :(得分:0)

我尝试了扩展程序,它像这样正常工作:

bodyLabel.attributedText = entry.attributedText
bodyLabel.setLineSpacing(lineSpacing: -20)
bodyLabel.sizeToFit()

如果这样不起作用,请检查标签的高度,然后尝试将内容设置为“填充”。

bodyLabel.contentMode = .scaleAspectFill

答案 1 :(得分:0)

bodyLabel的约束必须类似于

enter image description here

bottom constraint中的label调整为>=0