gpt4 book ai didi

ios - Swift @IBDesignable - @IBInspectable 多变量

转载 作者:行者123 更新时间:2023-12-03 18:13:59 26 4
gpt4 key购买 nike

我正在尝试为 UILabel 创建一个自定义类,这样我就可以从 Storyboard 中看到结果。我需要更改文本属性以创建大纲标签。

使用目前为止的代码,我可以设法做到这一点,但是我只能添加一个变量。

如果我有多个 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

如何使用多个变量?代码:

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)

}
}



}

最佳答案

正如我在评论中所说 - 您需要将每个变量放在不同的行中。这意味着您需要为它们声明 didSet。像这样:

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)
}

}

关于ios - Swift @IBDesignable - @IBInspectable 多变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38873034/

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