OGeek|极客世界-中国程序员成长平台

标题: ios - 有没有办法在 Storyboard 中添加缩写/速记/常量来代替字符串? [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 18:13
标题: ios - 有没有办法在 Storyboard 中添加缩写/速记/常量来代替字符串?

我有一个 @IBDesignable UILabel@IBInspectable 属性,其中之一是字体名称。

必须在 Storyboard 中输入长而复杂的字体名称既乏味又容易出错:

enter image description here

如果可能的话,我想做的是在代码中包含常量,例如:

let Text75Bold        = "NHaasGroteskTXStd-75Bd"

为了在 Storyboard 中可见,即:

enter image description here

这可能吗?

标签代码供引用:

@IBDesignable class CustomLabel: UILabel {

    @IBInspectable override var text: String? {
        didSet {
            decorate()
        }
    }

    @IBInspectable var fontSize:CGFloat = 15.0 {
        didSet {
            decorate()
        }
    }

    @IBInspectable var fontColor: UIColor = UIColor.red {
        didSet {
            decorate()
        }
    }

    @IBInspectable var tracking: Int = 0 {
        didSet {
            decorate()
        }
    }

    @IBInspectable var fontName: String = Display75Bold {
        didSet {
            decorate()
        }
    }

....



Best Answer-推荐答案


正如我们在评论中所讨论的,您可以使用短名称作为键、长名称作为值以及简单字符串作为可检查属性的字典(不可检查)。在该属性的 setter 中,您可以使用它作为该字典的键来更新字体。

关于ios - 有没有办法在 Storyboard 中添加缩写/速记/常量来代替字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40917651/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4