iPhone UITextField - 更改占位符文本顏色我想更改我在UITextField控件中設(shè)置的占位符文本的顏色,使其變黑。我更喜歡這樣做而不使用普通文本作為占位符,并且必須覆蓋所有方法來模仿占位符的行為。我相信如果我重寫這個方法:- (void)drawPlaceholderInRect:(CGRect)rect然后我應(yīng)該能夠做到這一點。但我不確定如何從此方法中訪問實際的占位符對象。
3 回答

慕標琳琳
TA貢獻1830條經(jīng)驗 獲得超9個贊
自從在iOS 6中的UIViews中引入屬性字符串以來,可以為占位符文本指定顏色,如下所示:
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.}

函數(shù)式編程
TA貢獻1807條經(jīng)驗 獲得超9個贊
容易和無痛,可能是一些容易的替代品。
_placeholderLabel.textColor
不建議生產(chǎn),Apple可能會拒絕您的提交。

MMTTMM
TA貢獻1869條經(jīng)驗 獲得超4個贊
您可以這樣覆蓋drawPlaceholderInRect:(CGRect)rect
以手動呈現(xiàn)占位符文本:
- (void) drawPlaceholderInRect:(CGRect)rect { [[UIColor blueColor] setFill]; [[self placeholder] drawInRect:rect withFont:[UIFont systemFontOfSize:16]];}
- 3 回答
- 0 關(guān)注
- 648 瀏覽
添加回答
舉報
0/150
提交
取消