UITextField在iOS7中的占位符始终顶部对齐

12

我已经对UITextField类进行了子类化,并编写了以下代码

- (void)drawPlaceholderInRect:(CGRect)rect
{

    [self.placeHolderTextColor setFill];
    [self.placeholder drawInRect:rect
                        withFont:self.placeHolderFont
                   lineBreakMode:NSLineBreakByTruncatingTail
                       alignment:NSTextAlignmentLeft];

}

我也写了一行代码:self.contentVerticalAlignment = UIControlContentVerticalAlignmentTop;

iOS6中,这个占位符文本居中对齐,但在iOS7中不是,它显示为顶部对齐。

虽然我输入的文本居中显示,但它只有占位符字符串出现问题。

我尝试了使用xib来设置占位符字符串,在XIB中它正常显示,但当我运行代码时,文本框的占位符就顶部对齐了。

有什么解决办法吗?

Vadoff的回答对我有用。以下是完整实现代码,可能会帮助到遇到同样问题的人。

drawInRect方法在iOS7中已经弃用,drawInRectWithAttributes可以使用。

- (void)drawPlaceholderInRect:(CGRect)rect
{

    [self.placeHolderTextColor setFill];

    CGRect placeholderRect = CGRectMake(rect.origin.x, (rect.size.height- self.placeHolderFont.pointSize)/2 - 2, rect.size.width, self.placeHolderFont.pointSize);
    rect = placeholderRect;

    if(iOS7) {

        NSMutableParagraphStyle* style = [[NSMutableParagraphStyle alloc] init];
        style.lineBreakMode = NSLineBreakByTruncatingTail;
        style.alignment = self.placeHolderTextAlignment;


        NSDictionary *attr = [NSDictionary dictionaryWithObjectsAndKeys:style,NSParagraphStyleAttributeName, self.placeHolderFont, NSFontAttributeName, self.placeHolderTextColor, NSForegroundColorAttributeName, nil];

        [self.placeholder drawInRect:rect withAttributes:attr];


    }
    else {
        [self.placeholder drawInRect:rect
                            withFont:self.placeHolderFont
                       lineBreakMode:NSLineBreakByTruncatingTail
                           alignment:self.placeHolderTextAlignment];
    }

}

我也有同样的问题,目前还没有解决方案。 - Vadoff
6个回答

16

在iOS7中,drawInRect方法似乎表现不同,您可以尝试添加以下行并将其用作要绘制的矩形,它还与早期版本的iOS兼容。

drawInRect方法在iOS7中表现不同,可以尝试添加以下代码并将其作为要绘制的矩形,这样可以向后兼容旧版本iOS。

  CGRect placeholderRect = CGRectMake(rect.origin.x, (rect.size.height- self.font.pointSize)/2, rect.size.width, self.font.pointSize);

9
一个小问题:在许多情况下,使用字体的lineHeight比使用pointSize更适合于居中、多行文本等情况。因此,在我的代码中,我一直在使用self.font.lineHeight来代替你使用的.pointSize。 - sujal

3
下面的代码适用于iOS 5/6/7。
@implementation PlaceholderTextField

- (void)drawPlaceholderInRect:(CGRect)rect
{
    // Placeholder text color, the same like default
    UIColor *placeholderColor = [UIColor colorWithWhite:0.70 alpha:1];
    [placeholderColor setFill];

    // Get the size of placeholder text. We will use height to calculate frame Y position
    CGSize size = [self.placeholder sizeWithFont:self.font];

    // Vertically centered frame
    CGRect placeholderRect = CGRectMake(rect.origin.x, (rect.size.height - size.height)/2, rect.size.width, size.height);

    // Check if OS version is 7.0+ and draw placeholder a bit differently
    if (IS_IOS7) {

        NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
        style.lineBreakMode = NSLineBreakByTruncatingTail;
        style.alignment = self.textAlignment;
        NSDictionary *attr = [NSDictionary dictionaryWithObjectsAndKeys:style,NSParagraphStyleAttributeName, self.font, NSFontAttributeName, placeholderColor, NSForegroundColorAttributeName, nil];

        [self.placeholder drawInRect:placeholderRect withAttributes:attr];


    } else {
        [self.placeholder drawInRect:placeholderRect
                            withFont:self.font
                       lineBreakMode:NSLineBreakByTruncatingTail
                           alignment:self.textAlignment];
    }

}

@end

1

我通过子类化UITextFieldClass并重写drawPlaceholderInRect函数来解决了这个问题。

 - (void)drawPlaceholderInRect:(CGRect)rect
{

    if(IS_IOS7)
    {
        [[self placeholder] drawInRect:CGRectMake(rect.origin.x, rect.origin.y+10, rect.size.width, rect.size.height) withFont:self.font];
    }
    else {

        [[self placeholder] drawInRect:rect withFont:self.font];
    }
}

0

为什么不直接移动绘制矩形,然后调用超类方法的实现呢?这里附上 Swift 代码...

override func drawPlaceholderInRect(rect: CGRect) {
    var newRect = CGRectInset(rect, 0, 2)
    newRect.origin.y += 2
    super.drawPlaceholderInRect(newRect)
}

0
一个小更新
- (void) drawPlaceholderInRect:(CGRect)rect {
    if (self.useSmallPlaceholder) {
        NSDictionary *attributes = @{
                                 NSForegroundColorAttributeName : kInputPlaceholderTextColor,
                                 NSFontAttributeName : [UIFont fontWithName:kInputPlaceholderFontName size:kInputPlaceholderFontSize]
                                 };

        //center vertically
        CGSize textSize = [self.placeholder sizeWithAttributes:attributes];
        CGFloat hdif = rect.size.height - textSize.height;
        hdif = MAX(0, hdif);
        rect.origin.y += ceil(hdif/2.0);

        [[self placeholder] drawInRect:rect withAttributes:attributes];
    }
    else {
        [super drawPlaceholderInRect:rect];
    }
}

http://www.veltema.jp/2014/09/15/Changing-UITextField-placeholder-font-and-color/


0

由于您设置了yourTextField.borderStyle = UITextBorderStyle....


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接