UITextField占位符字符串始终位于ios7中

我有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中它正在显示,但是当我运行代码textfield占位符是顶部对齐的.

任何解决方法

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

}

解决方法

drawInRect方法在iOS7中似乎表现不同,您可以尝试添加以下行,并将其用作直接绘制.它也向前兼容iOS7之前.
CGRect placeholderRect = CGRectMake(rect.origin.x,(rect.size.height- self.font.pointSize)/2,self.font.pointSize);

相关文章

当我们远离最新的 iOS 16 更新版本时,我们听到了困扰 Apple...
欧版/美版 特别说一下,美版选错了 可能会永久丧失4G,不过只...
一般在接外包的时候, 通常第三方需要安装你的app进行测...
前言为了让更多的人永远记住12月13日,各大厂都在这一天将应...