12

I have subclass the UITextField class and did the below code

- (void)drawPlaceholderInRect:(CGRect)rect
{

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

}

I have also written self.contentVerticalAlignment = UIControlContentVerticalAlignmentTop; line of code

This placeholder text is properly center aligned in ios6 but not in ios7 it is showing top aligned.

Although text I type is appearing centered.It only has issue with placeholder string.

I have tried with xib to set placeholder string.In XIB it is showing properly but when I run the code textfield placeholder is top aligned.

Any workaround for this?

Vadoff's answer worked for me. Still this is the full implementation that might help anyone with the same issue.

drawInRect method is deprecated in ios7 and drawInRectWithAttributes works

- (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];
    }

}
4

6 回答 6

16

drawInRect 方法在 iOS7 中的行为似乎有所不同,您可以尝试添加以下行并将其用作 rect 来绘制。它还向后兼容 iOS7 之前的版本。

  CGRect placeholderRect = CGRectMake(rect.origin.x, (rect.size.height- self.font.pointSize)/2, rect.size.width, self.font.pointSize);
于 2013-10-01T00:12:19.250 回答
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
于 2013-10-07T09:06:12.130 回答
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];
    }
}
于 2013-11-29T10:53:02.340 回答
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/

于 2014-09-15T06:21:44.990 回答
0

为什么不只是移动绘图矩形然后调用超级方法实现调用呢?Swift代码随之而来......

override func drawPlaceholderInRect(rect: CGRect) {
    var newRect = CGRectInset(rect, 0, 2)
    newRect.origin.y += 2
    super.drawPlaceholderInRect(newRect)
}
于 2014-12-23T11:17:42.007 回答
0

因为你已经设置了 yourTextField.borderStyle = UITextBorderStyle....

于 2015-09-11T04:19:52.203 回答