UILabel中的adjustsFontForContentSizeCategory具有属性文本

Gil*_*roy 9 nsattributedstring uilabel ios

我以为我会节省其他时间的调试,并说设置adjustsFontForContentSizeCategory为true对你设置attributedText值的标签没有影响(至少在tableview单元格标签中).

幸运的是,解决方案是自己在属性字符串上设置字体.我写了一个小的实用程序扩展:

public extension NSMutableAttributedString {
    public func setFont(_ font: UIFont) -> NSMutableAttributedString {
        addAttribute(NSAttributedStringKey.font, value: font, range: NSRange(location: 0, length: string.count))

        return self
    }
}
Run Code Online (Sandbox Code Playgroud)

在哪里设置您要调用的attributedText属性

@IBOutlet weak var myLabel: UILabel!

var myAttributedString: NSAttributedString = .....

myLabel.attributedText = NSMutableAttributedString(attributedString: myAttributedString).setFont(myLabel.font)
Run Code Online (Sandbox Code Playgroud)

Gil*_*roy 0

public extension NSMutableAttributedString {
public func setFont(_ font: UIFont) -> NSMutableAttributedString {
    addAttribute(NSAttributedStringKey.font, value: font, range: NSRange(location: 0, length: string.count))

    return self
}
Run Code Online (Sandbox Code Playgroud)

}