gpt4 book ai didi

ios - 扩展可能不包含 UItextfield 中的存储属性

转载 作者:行者123 更新时间:2023-11-28 06:09:49 24 4
gpt4 key购买 nike

extension UITextField
@IBInspectable var placeholdercolor: UIColor
{
willSet {
attributedPlaceholder = NSAttributedString(string: placeholder != nil ? placeholder! : "", attributes:[NSAttributedStringKey.foregroundColor: placeholdercolor])
}}

我正在为占位符颜色的 UITextField 创建扩展。我不想创建自定义类,我也尝试

@IBInspectable var placeholdercolor: UIColor 
{
get
{
return self.placeholdercolor
}
set
{
attributedPlaceholder = NSAttributedString(string: placeholder != nil ? placeholder! : "", attributes:[NSAttributedStringKey.foregroundColor: placeholdercolor])
}
}

但它在方法上给出错误(线程 1:EXC_BAD_ACCESS)

get
{
return self.placeholdercolor
}

请帮忙

最佳答案

@IBInspectable var placeholdercolor: UIColor 
{
get
{ // you can't return this here because this will call the getter again and again -> Stack Overflow
return self.placeholdercolor
}
set
{
attributedPlaceholder = NSAttributedString(string: placeholder != nil ? placeholder! : "", attributes:[NSAttributedStringKey.foregroundColor: placeholdercolor])
}
}

你应该在 getter 中返回属性字符串中的前景色属性:

get
{
return attributedPlaceholder?.attribute(NSForegroundColorAttributeName, at: 0, effectiveRange: nil) as? UIColor
}

我建议您将此属性设置为可选属性,以防未设置该属性。

编辑:

您的二传手也不正确。您应该使用 newValue:

attributedPlaceholder = NSAttributedString(string: placeholder != nil ?  placeholder! : "", attributes:[NSAttributedStringKey.foregroundColor: newValue])

关于ios - 扩展可能不包含 UItextfield 中的存储属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47069374/

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