[英]Swift @IBDesignable - @IBInspectable multiple variables
I am trying to create a custom class for UILabel
so I can see the result from the Storyboard. 我正在尝试为UILabel
创建一个自定义类,以便我可以从Storyboard中看到结果。 I need to change the text attributes in order to create an outline label. 我需要更改文本属性以创建轮廓标签。
With the code I have so far I can manage to do this however I can only add one variable. 到目前为止我的代码我可以设法做到这一点,但我只能添加一个变量。
If I had multiple var
I get the following errors. 如果我有多个var
我会收到以下错误。
> 'var' declarations with multiple variables cannot have explicit
> getters/setters 'var' cannot appear nested inside another 'var' or
> 'let' pattern Getter/setter can only be defined for a single variable
How can I use multiple variables? 我如何使用多个变量? CODE: 码:
import UIKit
@IBDesignable
class CustomUILabel: UILabel {
@IBInspectable var outlineWidth: CGFloat = 1.0, var outlineColor = UIColor.whiteColor() {
didSet {
let strokeTextAttributes = [
NSStrokeColorAttributeName : outlineColor,
NSStrokeWidthAttributeName : -1 * outlineWidth,
]
self.attributedText = NSAttributedString(string: self.text ?? "", attributes: strokeTextAttributes)
}
}
}
As I said in the comment - you need to have each variable in separate lines. 正如我在评论中所说 - 你需要将每个变量放在不同的行中。 This means that you need to have didSet
declared for both of them. 这意味着您需要为它们didSet
声明didSet
。 Something like this : 像这样的东西:
import UIKit
@IBDesignable
class CustomUILabel: UILabel {
@IBInspectable var outlineWidth: CGFloat = 1.0 {
didSet {
self.setAttributes(self.outlineColor, outlineWidth: self.outlineWidth)
}
}
@IBInspectable var outlineColor = UIColor.whiteColor() {
didSet {
self.setAttributes(self.outlineColor, outlineWidth: self.outlineWidth)
}
}
func setAttributes(outlineColor:UIColor, outlineWidth: CGFloat) {
let strokeTextAttributes = [
NSStrokeColorAttributeName : outlineColor,
NSStrokeWidthAttributeName : -1 * outlineWidth,
]
self.attributedText = NSAttributedString(string: self.text ?? "", attributes: strokeTextAttributes)
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.