繁体   English   中英

如何更改物镜c占位符的颜色

[英]how to change the color of placeholder of objective c

我想在目标c中更改UItextfield占位符颜色,但是我的类是uiviewcontroller的子类。 我可以继承uiviewcontroller和uitextfield吗?

Swift 4.0及更高版本

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

目标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”];

您需要将UITextField子类化,并使用drawPlaceholderInRect方法

看这个问题

您可以使用以下代码

[yourtextfield setValue:[UIColor colorWithRed:120.0/255.0 green:116.0/255.0 blue:115.0/255.0 alpha:1.0] forKeyPath:@"_placeholderLabel.textColor"];

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM