在界面构建器中,我可以像这样简单地为不同的大小类设置不同的字体大小:
我现在正在重写代码中的UILabel
,以使用NSAttributedString
,但是如何为不同的尺寸类别添加不同的字体大小?
@IBOutlet weak var instructionsLabel2: UILabel! {
didSet {
let attString = NSMutableAttributedString()
let dict1:[NSAttributedString.Key:Any] = [
.font: UIFont.systemFont(ofSize: 18, weight: .medium)
]
attString.append(NSAttributedString(string: "Line 1 text\n", attributes: dict1))
let dict2:[NSAttributedString.Key:Any] = [
.font: UIFont.systemFont(ofSize: 12, weight: .medium)
]
attString.append(NSAttributedString(string: "Line 2 text", attributes: dict2))
instructionsLabel2.attributedText = attString
}
}