@IBInspectable与NSAttributedString

时间:2017-08-14 14:13:27

标签: swift nsattributedstring ibinspectable

我有以下情况

@IBInspectable var image : UIImage {
    get {
        return imageView.image ?? UIImage()
    }

    set {
        imageView.image = newValue
    }
}

@IBInspectable var firstText : String {
    get {
        return menButton.title(for: .normal) ?? ""
    }

    set {
        menButton.setAttributedTitle(NSAttributedString.init(string: newValue, attributes: self.selectedAttribute), for: .normal)
    }
}

第一个只是通过Storyboard设置一个简单的图像,效果很好。

我一直在为NSAttributedString的属性尝试相同但它不起作用。

我还尝试创建一个新的NSAttributedString并将其设置为按钮的attributionString但没有任何改变。

另一件事:我更喜欢使用getter和setter语法而不是didSet

更新

@IBDesignable class MenuControl: UIView {

private lazy var selectedAttribute : [String : Any] = {
    return [NSForegroundColorAttributeName: UIColor.black,NSFontAttributeName : UIFont(name: "Futura PT", size: 15) ?? UIFont.systemFont(ofSize: 15, weight: UIFontWeightBold)]
}()

private var unselectedAttribut : [String : Any] = {
    return [NSForegroundColorAttributeName: UIColor.lightGray ,NSFontAttributeName : UIFont(name: "Futura PT", size: 15) ?? UIFont.systemFont(ofSize: 15, weight: UIFontWeightBold)]
}()

private lazy var menButton : UIButton = {
        let menButton = UIButton(type: .system)
        menButton.translatesAutoresizingMaskIntoConstraints = false


        menButton.addTarget(self, action: #selector(handleSelection), for: .touchUpInside)
        menButton.setAttributedTitle(NSAttributedString.init(string: "Men", attributes: self.selectedAttribute), for: .normal)
        return menButton
    }()

}

0 个答案:

没有答案