gpt4 book ai didi

ios - iPhone UITextField - 更改占位符文本颜色

转载 作者:IT王子 更新时间:2023-10-29 07:24:23 26 4
gpt4 key购买 nike

我想更改我在 UITextField 控件中设置的占位符文本的颜色,使其变为黑色。

我更愿意在不使用普通文本作为占位符的情况下执行此操作,并且不必重写所有方法来模仿占位符的行为。

我相信如果我覆盖这个方法:

- (void)drawPlaceholderInRect:(CGRect)rect

那么我应该可以做到这一点。但我不确定如何从此方法中访问实际的占位符对象。

最佳答案

自从在 iOS 6 的 UIView 中引入了属性字符串,就可以像这样为占位符文本分配颜色:

if ([textField respondsToSelector:@selector(setAttributedPlaceholder:)]) {
UIColor *color = [UIColor blackColor];
textField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:placeholderText attributes:@{NSForegroundColorAttributeName: color}];
} else {
NSLog(@"Cannot set placeholder text's color, because deployment target is earlier than iOS 6.0");
// TODO: Add fall-back code to set placeholder color.
}

关于ios - iPhone UITextField - 更改占位符文本颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1340224/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com