获取UITextField.placeHolder属性的前景色?

时间:2016-09-11 23:33:34

标签: ios swift2 nsattributedstring swift3

我正在尝试获取UITextField.placeHolder对象的前景色。我必须使用NSAttributeString的以下成员之一:

attributesAtIndex(_:effectiveRange:)
attributesAtIndex(_:longestEffectiveRange:inRange:)
attribute(_:atIndex:effectiveRange:)
attribute(_:atIndex:longestEffectiveRange:inRange:)

关于如何设置NSAttributeString的属性,有一百万个例子,

uiTextFieldObject.attributedPlaceholder = 
   NSAttributedString(string:"placeholder text", attributes[NSForegroundColorAttributeName: UIColor.redColor()])

但我找不到一个检索NSAttributeString属性值的简单示例。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

试试这个 - 放置添加验证

夫特

D

的Objective-C

let attributedString = NSAttributedString(string: "Temp String",
                                              attributes: [NSForegroundColorAttributeName: UIColor.redColor()])

    attributedString.enumerateAttribute(NSForegroundColorAttributeName,
                                        inRange: NSRange(location: 0, length: attributedString.length), options:NSAttributedStringEnumerationOptions(rawValue: 0)){(attribute, range, other) in

        if let color = attribute as? UIColor {
            print("color \(color)")
        }
    }