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);

以上是来客网为你收集整理的UITextField占位符字符串始终位于ios7中全部内容,希望文章能够帮你解决UITextField占位符字符串始终位于ios7中所遇到的程序开发问题。

如果觉得来客网网站内容还不错,欢迎将来客网网站推荐给程序员好友。