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

前端之家收集整理的这篇文章主要介绍了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);
原文链接:https://www.f2er.com/iOS/336116.html

猜你在找的iOS相关文章