gpt4 book ai didi

ios - 在 IBDesignableTextField 上的何处设置颜色

转载 作者:行者123 更新时间:2023-11-30 13:50:20 25 4
gpt4 key购买 nike

我有一个问题,建议在哪里设置我已子类化的 UITextField 的颜色。这是一些代码。

@IBDesignable
class TextField: UITextField {

//var inset is not important for this question.
@IBInspectable var inset: CGFloat = 10

override init(frame: CGRect) {
super.init(frame: frame)
//tried setting color here, didn't show up in storyboard
}

required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
//tried setting color here, didn't show up in storyboard
}

override func drawRect(rect: CGRect) {
super.drawRect(rect)
//tried setting color here, didn't show up in storyboard
}

override func textRectForBounds(bounds: CGRect) -> CGRect {
self.backgroundColor = UIColor.redColor()
//HERE IT WORKS!!!
return CGRectInset(bounds, inset, inset)
}

override func editingRectForBounds(bounds: CGRect) -> CGRect {
return textRectForBounds(bounds)
}
}

所以,我基本上想做的是编写 UITextField 的子类,它设置 Storyboard中显示的文本字段(以及将来的其他内容)的背景颜色。我唯一可以让它工作的地方是在 textRectForBounds: 函数上,我必须为文本设置填充。我的问题是,建议在哪里设置 UITextField 的背景颜色并使该更改显示在 Storyboard 中?谢谢。

最佳答案

我让它工作的方法是添加这些方法......

#if !TARGET_INTERFACE_BUILDER
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)!
self._setup()
}
#endif

override func prepareForInterfaceBuilder() {
self._setup()
}

func _setup() {
//do all the setup here to make the appearance show up in Interface builder and app.
}

关于ios - 在 IBDesignableTextField 上的何处设置颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34374725/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com