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

您可以使用以下代码

[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 回答
0

您需要继承 UITextField 并覆盖该drawPlaceholderInRect 方法

看这个问题

于 2012-07-19T08:03:03.617 回答