【问题标题】:UITextField placeholder string is always Top aligned in ios7UITextField占位符字符串在ios7中总是顶部对齐
【发布时间】:2013-10-06 06:43:08
【问题描述】:

我有 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 中它显示正确,但是当我运行代码时,文本字段占位符顶部对齐。

有什么解决方法吗?

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

}

【问题讨论】:

  • 我也有同样的问题,还没有解决办法。

标签: uitextfield ios7


【解决方案1】:

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

【讨论】:

    【解决方案2】:

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

    override func drawPlaceholderInRect(rect: CGRect) {
        var newRect = CGRectInset(rect, 0, 2)
        newRect.origin.y += 2
        super.drawPlaceholderInRect(newRect)
    }
    

    【讨论】:

      【解决方案3】:

      小幅更新

      - (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/

      【讨论】:

        【解决方案4】:

        我已经通过继承 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];
            }
        }
        

        【讨论】:

          【解决方案5】:

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

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

          【讨论】:

          • 一个小问题:在许多情况下,lineHeight 将返回更好的结果,而不是使用字体的 pointSize,比如居中、多行文本等。所以,在我的代码中,我一直在你使用 .pointSize 的任何地方都使用 self.font.lineHeight
          【解决方案6】:

          下面的代码适用于 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
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2015-12-31
            • 2014-02-16
            • 2016-09-24
            • 1970-01-01
            • 2014-07-23
            • 2011-06-18
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多