1

目的の c で UItextfield プレースホルダーの色を変更したいのですが、私のクラスは uiviewcontroller のサブクラスです。uiviewcontroller と uitextfield を継承できますか?

4

3 に答える 3

1

Swift 4.0 以降

let text = textField.placeholder ?? ""
self.attributedPlaceholder = NSAttributedString(string: text, attributes: [NSAttributedString.Key.foregroundColor: UIColor.red])

Objective-C

NSString *text = textField.text;
if (!text) {
    text = @"";
}
textField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:text attributes:@{NSForegroundColorAttributeName: [UIColor redColor]}];

古い Objective-C コード

[self.textfield setValue:[UIColor darkGrayColor] forKeyPath:@"_placeholderLabel.textColor”];
于 2012-07-19T07:54:06.457 に答える
0

UITextField をサブクラス化し、drawPlaceholderInRect メソッドをオーバーライドする必要があります

この質問を見て

于 2012-07-19T08:03:03.617 に答える
0

以下のコードを使用できます

[yourtextfield setValue:[UIColor colorWithRed:120.0/255.0 green:116.0/255.0 blue:115.0/255.0 alpha:1.0] forKeyPath:@"_placeholderLabel.textColor"];
于 2014-07-03T06:34:41.670 に答える