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

プレースホルダーカラー用のUITextFieldの拡張機能を作成しています。私は、カスタムクラスを作成したくないと私はまた、拡張機能には、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 
    } 

答えて

1
@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]) 
関連する問題