本文介绍了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中未正确对齐它显示为顶部对齐。



虽然我输入的文字显示为居中。它只有占位符字符串问题。





这有什么解决方法吗?



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
对准:self.placeHolderTextAlignment];
}

}


解决方案

drawInRect方法在iOS7中的行为似乎有所不同,您可以尝试添加以下行并将其用作矩形来代替。它也向前兼容iOS7之前。

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


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

}
解决方案

drawInRect methods seem to behave differently in iOS7, you can try adding the following line and use that as the rect to draw instead. It's also backwards compatible with pre-iOS7.

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

这篇关于UITextField占位符字符串在ios7中始终是顶部对齐的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 22:09