【发布时间】:2013-09-25 19:05:42
【问题描述】:
我尝试继承 UITextField 来绘制自定义占位符。在iOS 6 中这工作正常,但在iOS 7 中我得到了不同的CGRect 高度。
UITextField 框架是 (0, 0, 500, 45)。我通过覆盖添加了 20 的左填充
- (CGRect)editingRectForBounds:(CGRect)bounds;
- (CGRect)placeholderRectForBounds:(CGRect)bounds;
- (CGRect)textRectForBounds:(CGRect)bounds;
调用以下方法:
- (CGRect)makeRectFromBounds:(CGRect)bounds
withTopPadding:(CGFloat)topPadding
andLeftPadding:(CGFloat)leftPadding
{
return UIEdgeInsetsInsetRect(bounds, UIEdgeInsetsMake(topPadding, leftPadding, 0, 0));
}
因为我想要不同的 placeHolder 文本颜色,所以我覆盖了
- (void)drawPlaceholderInRect:(CGRect)rect
- (void)drawPlaceholderInRect:(CGRect)rect {
[[UIColor colorWithRed:121.0/255.0
green:113.0/255.0
blue:107.0/255.0
alpha:1.0] setFill];
[self printRect:rect from:__FUNCTION__];
[[self placeholder] drawInRect:rect withFont:self.font];
}
我打印的矩形如下:
iOS 7: -Rect (X: 0.0, Y:0.0, W:480.0, H:44.0)
iOS 6: -Rect (X: 0.0, Y:0.0, W:480.0, H:26.0)
知道这是一个错误还是我做错了什么?
【问题讨论】:
-
我遇到了同样的问题,对我来说最好的解决方案是:Set custom color and draw placeholder vertically centered on iOS 5/6/7
标签: iphone ios objective-c uitextfield placeholder