繁体   English   中英

UITextField - (void)drawPlaceholderInRect:(CGRect)rect在iOS 7中返回不同的CGRect高度

[英]UITextField - (void)drawPlaceholderInRect:(CGRect)rect returns different CGRect height in iOS 7

我试图将UITextField子类化为绘制自定义placehoder。 iOS 6这种方法很好但在iOS 7我得到了不同的CGRect高度。

UITextField框架为(0, 0, 500, 45) UITextField (0, 0, 500, 45) 我通过覆盖- (CGRect)editingRectForBounds:(CGRect)bounds;添加了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)

不知道这是一个错误还是我做错了什么?

请改用以下内容:

[textfield setValue:[UIColor redColor] forKeyPath:@"_placeholderLabel.textColor"];

在iOS 7中, contentVerticalAlignment的默认值从“top”更改为“center”(没有我可以看到的文档)。 在“中心”模式下,iOS会在绘制之前调整rectForBounds方法的结果。 在覆盖任何textRectForBounds方法时,您应该设置contentVerticalAlignment = UIControlContentVerticalAlignmentTop ,以便iOS将完全按照指定使用rect。

由于iOS7现在是新的,所以很多人都面临着iOS7的框架问题。

对于所有这些人,我只想说它很容易,iOS7没有任何问题。 这只是因为您不知道如何从Apple提供的最新操作系统功能中获益。

@Cyupa:您只需要应用自动调整大小并屏蔽您的文本字段。

它可能是以下一个或多个。

  • UIViewAutoresizingFlexibleBottomMargin
  • UIViewAutoresizingFlexibleTopMargin
  • UIViewAutoresizingFlexibleLeftMargin
  • UIViewAutoresizingFlexibleRightMargin
  • UIViewAutoresizingFlexibleHeight
  • UIViewAutoresizingFlexibleWidth

如果您在文本字段中应用适当的自动调整遮罩,您将获得所需的视图框架(此处为textfield)

我也遇到了这个问题,还没找到原因,但是如果你想在iOS6和iOS7上都有相同的行为,你可以试试这个:

- (CGRect)textRectForBounds:(CGRect)bounds {
    CGRect rect = [super textRectForBounds:bounds];
    rect = CGRectMake(20, rect.origin.y-4, rect.size.width-20, rect.size.height);
    return rect;
}

你可能需要设置:

theLabel.contentVerticalAlignment =UIControlContentVerticalAlignmentCenter;

检查系统版本并返回UIEdgeInsetsInsetRect(bounds, UIEdgeInsetsMake(topPadding, leftPadding, 0, 0));

你可以检查设备版本

if(([[[UIDevice currentDevice] systemVersion] floatValue]>=7.0))
{

}

我通过使用此属性解决了问题

textField.contentVerticalAlignment =UIControlContentVerticalAlignmentCenter;

它适用于iOS6和iOS7。

Ricky的解决方案对我有用,添加此值必须在每次占位符文本更改后设置。 我覆盖了setPlaceholder来做到这一点。

它取代了覆盖drawPlaceholderInRect的需要,如果你想要另一个占位符颜色,因此垂直对齐将自动正确。 当然,这并没有回答这个问题,为什么IOS 7.0表现得那样,但它可能会解决你的实际问题。

- (void) setPlaceholder: (NSString*)placeholderText {

    [super setPlaceholder:placeholderText];

    [self setValue:[UIColor redColor] forKeyPath:@"_placeholderLabel.textColor"];
}

应该提到的是,有些人不鼓励绕过公共接口,所以请自行承担风险! 另请参阅:以编程方式更改UITextField的占位符文本颜色

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM