iOS开发 修改UITextField的placeholder的颜色 oc代码
在iOS开发中,要修改UITextField的placeholder的颜色,可以通过设置UITextField的attributedPlaceholder属性来实现。下面是OC代码示例:
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(20, 50, 200, 30)];
textField.placeholder = @"请输入内容";
textField.textColor = [UIColor blackColor];
NSDictionary *attributes = @{
NSForegroundColorAttributeName: [UIColor redColor],
NSFontAttributeName: [UIFont systemFontOfSize:14.0]
};
NSAttributedString *attributedPlaceholder = [[NSAttributedString alloc] initWithString:textField.placeholder attributes:attributes];
textField.attributedPlaceholder = attributedPlaceholder;
[self.view addSubview:textField];
在上述代码中,通过设置NSDictionary对象attributes的NSForegroundColorAttributeName属性来设置placeholder的颜色,可以根据需要设置其他属性,如字体大小等。然后通过NSAttributedString对象attributedPlaceholder将attributes应用到UITextField的placeholder上,最后将attributedPlaceholder赋值给UITextField的attributedPlaceholder属性即可。
希望对你有所帮助
原文地址: https://www.cveoy.top/t/topic/iCz0 著作权归作者所有。请勿转载和采集!