iOS @IBDesignable 自动刷新 UILabel 参数为标题、字体、颜色

Posted

技术标签:

【中文标题】iOS @IBDesignable 自动刷新 UILabel 参数为标题、字体、颜色【英文标题】:iOS @IBDesignable autorefresh UILabel params as title, font, color 【发布时间】:2017-08-17 07:57:29 【问题描述】:

我正在开发一个功能,我可以在 UILabel 上设置我的自定义样式(通过录制样式名称)。我有一个简单的代码:

struct Style 
    var fontName: String!
    var fontSize: CGFloat!
    var color: UIColor!
    var styleName: String!
    var font: UIFont 
        return UIFont(name: self.fontName, size: self.fontSize)!
    

    init(styleName: String, fontName: String!, fontSize: CGFloat!, color: UIColor!) 
        self.fontName = fontName
        self.fontSize = fontSize
        self.color = color
    

    var desc: String 
        return "Style: \(styleName)"
    




let dictStyle: [String: Style] = [
                "normal": Style(styleName: "normal", fontName: "HelveticaNeue-Light", fontSize: 20, color: UIColor.red),
                "bold": Style(styleName: "bold", fontName: "HelveticaNeue-Bold", fontSize: 30, color: UIColor.black)
            ]

@IBDesignable class Label: UILabel 

    @IBInspectable var style: String = "normal" 
        didSet 
            setNeedsDisplay()
        
    

    override func prepareForInterfaceBuilder() 
        super.prepareForInterfaceBuilder()

        self.text = "\(style)" // there is normal or bold

        if let color = dictStyle[self.style]?.color 
            self.textColor =  color
        
        if let font = dictStyle[self.style]?.font 
            self.font = font
        
        if let name = dictStyle[self.style]?.styleName 
            self.text =  name
        
    

它工作正常,但我想在 Interface Builder 中自动更改标题、字体、颜色(在下面的红色矩形中)。

我在 .storyboard 文件中看到过

<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Label" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="9lO-cw-I2Z" customClass="Label" customModule="AutoRefresh" customModuleProvider="target">
                            <rect key="frame" x="24" y="44"  />
                            <fontDescription key="fontDescription" name="HelveticaNeue" family="Helvetica Neue" pointSize="32"/>
                            <color key="textColor" red="0.25098040700000002" green="0.50196081400000003" blue="0.0" alpha="0.76206656679999996" colorSpace="calibratedRGB"/>
                            <nil key="highlightedColor"/>
                            <userDefinedRuntimeAttributes>
                                <userDefinedRuntimeAttribute type="string" keyPath="style" value="normal"/>
                            </userDefinedRuntimeAttributes>
                        </label>

所以“text”或“textColor”字段不会改变。

【问题讨论】:

【参考方案1】:

这是您在 Swift 4 中寻找的内容:

@IBDesignable
class Label: UILabel 

    override func awakeFromNib() 
        super.awakeFromNib()
        config()
    

    override func prepareForInterfaceBuilder() 
        super.prepareForInterfaceBuilder()
        config()
    

    func config() 
        // Setup how you please, e.g. color = UIColor.black
    


【讨论】:

以上是关于iOS @IBDesignable 自动刷新 UILabel 参数为标题、字体、颜色的主要内容,如果未能解决你的问题,请参考以下文章

使用Swift在Xcode 9中不显示IBDesignable UI

iOS开发UI篇—实现UItableview控件数据刷新

iOS开发UI篇—实现UITableview控件数据刷新

@IBDesignable 错误:无法更新自动布局状态

iOS - 无法让 @IBDesignable .xib 文件显示在情节提要或模拟器上

@IBDesignable错误:无法更新自动布局状态