gpt4 book ai didi

ios - UILabel 内的 lineSpacing 属性无法按预期工作

转载 作者:行者123 更新时间:2023-11-28 13:08:42 25 4
gpt4 key购买 nike

我正在尝试创建一个自定义 UILabel 类,它允许我增加 UILabel 上的行间距。我知道您可以在 IB 中使用属性文本字符串执行此操作,但是如果您使用自定义字体,则它不起作用。这是我的类(class)代码:

import UIKit

@IBDesignable
class SpacingLabel: UILabel
{

@IBInspectable var lineSpacing: CGFloat = 10.0

override func awakeFromNib()
{
self.renderText()
}

override func prepareForInterfaceBuilder()
{
super.prepareForInterfaceBuilder()
self.renderText()
}

func renderText()
{
var attrString = NSMutableAttributedString(string:self.text!)

if font != nil
{
NSMutableParagraphStyle.defaultParagraphStyle().mutableCopy()
var paragraphStyle = NSMutableParagraphStyle.defaultParagraphStyle().mutableCopy() as! NSMutableParagraphStyle
paragraphStyle.textAlignment = self.textAlignment

paragraphStyle.lineSpacing = self.lineSpacing
paragraphStyle.paragraphSpacing = self.lineSpacing

attrString.addAttributes([NSFontAttributeName : self.font!, NSParagraphStyleAttributeName : paragraphStyle], range: NSMakeRange(0, attrString.length))
self.attributedText = attrString
}

self.needsUpdateConstraints()
}

}

这是它在 IB( Storyboard)中的呈现方式:

enter image description here

下面是它在模拟器中的呈现方式:

enter image description here

我试过添加 minimumLineHeight 和/或 maximumLineHeight 属性,但这些似乎在其他方面搞砸了......

最佳答案

所以...原来属性 lineSpacingUILabel 中可能的私有(private)变量/属性有某种冲突。我将属性重命名为 leading,现在它运行良好。

关于ios - UILabel 内的 lineSpacing 属性无法按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32008465/

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