我尝试过设置UITextField
“占位符”颜色为深色。
NSAttributedString * search = [[NSAttributedString alloc] initWithString:@"Search" attributes:@{NSForegroundColorAttributeName: [UIColor blackColor]}];
textField.attributedPlaceholder = search;
我也尝试过另一种方法
[textField setValue:[UIColor blackColor] forKeyPath:@"_placeholderLabel.textColor"];
但这两种方法都适用iOS 7,但没有致力于iOS 6.
- 可以设置暗吗“占位符”颜色为
UITextField
in iOS 6 target?
Thanks!
正如 Apple 所建议的,子类化 UITextField 并覆盖- (void)drawPlaceholderInRect:(CGRect)rect
是要走的路:
- (void)drawPlaceholderInRect:(CGRect)rect {
UIColor *colour = [UIColor lightGrayColor];
if ([self.placeholder respondsToSelector:@selector(drawInRect:withAttributes:)])
{ // iOS7 and later
NSDictionary *attributes = @{NSForegroundColorAttributeName: colour, NSFontAttributeName: self.font};
CGRect boundingRect = [self.placeholder boundingRectWithSize:rect.size options:0 attributes:attributes context:nil];
[self.placeholder drawAtPoint:CGPointMake(0, (rect.size.height/2)-boundingRect.size.height/2) withAttributes:attributes]; }
else { // iOS 6
[colour setFill];
[self.placeholder drawInRect:rect withFont:self.font lineBreakMode:NSLineBreakByTruncatingTail alignment:self.textAlignment];
}
}
Credit: http://www.brightec.co.uk/blog/how-change-colour-uitextfields-placeholder-text-ios7-and-still-support-ios6 http://www.brightec.co.uk/blog/how-change-colour-uitextfields-placeholder-text-ios7-and-still-support-ios6
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)