UILabel NSAttributedString字体大小基于大小类

时间:2020-04-24 11:00:09

标签: swift uikit nsattributedstring size-classes

在界面构建器中,我可以像这样简单地为不同的大小类设置不同的字体大小:

enter image description here

我现在正在重写代码中的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
    }
}

0 个答案:

没有答案