iOS UITextField Placeholder 文字颜色设置 (Objective-C 代码示例)
在 iOS 开发中,可以使用 'attributedPlaceholder' 属性来设置 UITextField 的 placeholder 文字的颜色。以下是使用 Objective-C 代码设置 UITextField placeholder 文字颜色的示例:
// 创建 UITextField 对象
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(10, 10, 200, 30)];
// 创建富文本属性字典
NSMutableDictionary *placeholderAttributes = [[NSMutableDictionary alloc] init];
[placeholderAttributes setObject:[UIColor redColor] forKey:NSForegroundColorAttributeName];
// 创建带有富文本属性的 placeholder 文字
NSAttributedString *attributedPlaceholder = [[NSAttributedString alloc] initWithString:'请输入' attributes:placeholderAttributes];
// 设置 UITextField 的 attributedPlaceholder 属性
textField.attributedPlaceholder = attributedPlaceholder;
// 添加 UITextField 到视图中
[self.view addSubview:textField];
在上述代码中,我们首先创建了一个 UITextField 对象,并设置了它的 frame。然后,创建了一个包含富文本属性的字典 'placeholderAttributes',将颜色设置为红色。接着,使用 'initWithString:attributes:' 方法创建了一个带有富文本属性的 placeholder 文字。最后,将创建的 'attributedPlaceholder' 赋值给 UITextField 的 'attributedPlaceholder' 属性,从而设置了 placeholder 文字的颜色为红色。最后,将 UITextField 添加到视图中。
请注意,上述代码中的颜色设置为红色只是示例,你可以根据需要修改为其他颜色。
原文地址: https://www.cveoy.top/t/topic/qvko 著作权归作者所有。请勿转载和采集!