要设置 UITextField 的 placeholder 颜色,需要使用 NSAttributedString 来创建富文本,并将其赋值给 UITextField 的 attributedPlaceholder 属性。以下是一个示例的 Objective-C 代码:

// 创建富文本
NSAttributedString *attributedPlaceholder = [[NSAttributedString alloc] initWithString:'Placeholder' attributes:@{NSForegroundColorAttributeName: [UIColor redColor]}];

// 设置 UITextField 的 attributedPlaceholder 属性
textField.attributedPlaceholder = attributedPlaceholder;

在上面的代码中,通过创建一个 NSAttributedString 对象,并使用 NSForegroundColorAttributeName 键来设置文本颜色为红色。然后,将创建的富文本赋值给 UITextField 的 attributedPlaceholder 属性,以设置 placeholder 的颜色为红色。

请注意,这段代码需要在 UITextFiled 被创建后调用,例如在视图控制器的 viewDidLoad 方法中。

iOS UITextField Placeholder 颜色设置 - Objective-C 代码示例

原文地址: https://www.cveoy.top/t/topic/o7Y6 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录