CAN
CAN

Reputation: 1717

Extension NSAttributedString Issue

I've lots of UITextfield and I want add a placeholder with different color. I'm trying to add extension instead of repeating the code every time to make it clean. I've written something like this but couldn't continue.

My code for attributedPlaceholder is

    emailTextField.attributedPlaceholder = NSAttributedString(string:"EMAIL ADDRESS",
        attributes:[NSForegroundColorAttributeName: UIColor.redColor()])

My extension:

import Foundation
import UIKit

extension NSAttributedString {

class func attributedPlaceholderString(string: String, withColor color: UIColor) -> NSAttributedString {

    let attributedString =  NSAttributedString(string:string, attributes:[NSForegroundColorAttributeName: color])
    return attributedString
    }

}

but when I write

emailTextField.attributedPlaceholder. 

I can't get the name of the class that I have created in the extension. Please where would be my issue?

Upvotes: 0

Views: 1059

Answers (1)

ajpallares
ajpallares

Reputation: 787

The code would be

emailTextField.attributedPlaceholder = NSAttributedString.attributedPlaceholderString("EMAIL ADDRESS", withColor: UIColor.redColor())

You have to take into account that you have created a class method (by using the class command), so you have to call that method on the class NSAttributedString

Upvotes: 2

Related Questions