gpt4 book ai didi

ios - 快速改变行距留下底部空白

转载 作者:行者123 更新时间:2023-11-29 05:40:03 25 4
gpt4 key购买 nike

我正在尝试更改标签的行间距以减少阿拉伯语的行间距,但它太多了。我使用的扩展函数来自here阿拉伯样式的添加正在努力控制标签的行间距,但唯一的问题是它留下底部边距空白,我认为在减少行间距之前等于相同的标签大小。

这里的扩展函数:

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

最佳答案

我尝试了该扩展,它工作正常,如下所示:

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

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

bodyLabel.contentMode = .scaleAspectFill

关于ios - 快速改变行距留下底部空白,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56646113/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com