iOS @IBDesignable autorefresh UILabel params作为标题,字体,颜色

时间:2017-08-17 07:57:30

标签: ios storyboard uilabel interface-builder ibdesignable

我正在开发一项功能,我可以在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中自动更改标题,字体和颜色(在下面的红色矩形中)。

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" width="327" height="24"/>
                            <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 个答案:

答案 0 :(得分:0)

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

       $(document).ready(function () {
        $(".lichild").parent().hide();
        $(".limain").click(function () {
            $(this).children('ul').show();
            $(this).siblings().find('ul').hide();
        });
        $(".lichild").click(function () {
            $(this).children('ul').show();
            $(this).siblings('li').find('ul').hide()

        });
    });