2016-09-11 4 views
0

Ich versuche, die Vordergrundfarbe eines UITextField.placeHolder-Objekts zu erhalten. Ich habe eine der folgenden Mitglieder der NSAttributeString verwenden:Vordergrundfarbe der UITextField.placeHolder-Eigenschaft?

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

Es gibt eine Million Beispiele, wie Attribute einen NSAttributeString zu setzen,

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

Aber ich nicht ein einfaches Beispiel, dass abruft finden ein NSAttributeString-Attributwert Jede Hilfe würde sehr geschätzt werden.

Antwort

0

Versuchen Sie, diese - Ort hinzufügen Validierungen

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); 
    } 
}]; 
+0

D-Griffin, danke. Ich bin ein erfahrener Programmierer, aber neu in Swift und iOS; Objective-C ist für mich wie eine Fremdsprache. Ich werde es versuchen! – gangelo

Verwandte Themen