带有属性文本的 UILabel 中的 adjustsFontForContentSizeCategory
adjustsFontForContentSizeCategory in UILabel with attributed text
我想我会节省其他人的调试时间,并说将 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
}
}
设置属性文本的位置 属性 您将调用
@IBOutlet weak var myLabel: UILabel!
var myAttributedString: NSAttributedString = .....
myLabel.attributedText = NSMutableAttributedString(attributedString: myAttributedString).setFont(myLabel.font)
public extension NSMutableAttributedString {
public func setFont(_ font: UIFont) -> NSMutableAttributedString {
addAttribute(NSAttributedStringKey.font, value: font, range: NSRange(location: 0, length: string.count))
return self
}
}
我想我会节省其他人的调试时间,并说将 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
}
}
设置属性文本的位置 属性 您将调用
@IBOutlet weak var myLabel: UILabel!
var myAttributedString: NSAttributedString = .....
myLabel.attributedText = NSMutableAttributedString(attributedString: myAttributedString).setFont(myLabel.font)
public extension NSMutableAttributedString {
public func setFont(_ font: UIFont) -> NSMutableAttributedString {
addAttribute(NSAttributedStringKey.font, value: font, range: NSRange(location: 0, length: string.count))
return self
}
}