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

Get foreground color of UITextField.placeHolder property?

我正在尝试获取 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 属性值的简单示例。任何帮助将不胜感激。

试试这个 - 放置添加验证

Swift

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

Objective-C

NSAttributedString * attributedString = [[NSAttributedString alloc]
                                   initWithString:@"Temp String"
                                   attributes:@{NSForegroundColorAttributeName:[UIColor redColor]}];

[attributedString enumerateAttribute:NSForegroundColorAttributeName
                inRange:NSMakeRange(0, attributedString.length)
                options:0 usingBlock:^(id value, NSRange range, BOOL *stop) {
    if (value) {
        UIColor *color = (UIColor *)value;
        NSLog(@"Color : %@", color);
    }
}];