UITextField placeholder string is always Top aligned in ios7

后端 未结 6 2534
滥情空心
滥情空心 2021-02-20 00:44

I have subclass the UITextField class and did the below code

- (void)drawPlaceholderInRect:(CGRect)rect
{

    [self.placeHolderTextColor setFill];
    [self.pl         


        
相关标签:
6条回答
  • 2021-02-20 01:09

    I have fixed this problem by subclassing UITextFieldClass and override drawPlaceholderInRect function.

     - (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];
        }
    }
    
    0 讨论(0)
  • 2021-02-20 01:14

    The code bellow works on 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
    
    0 讨论(0)
  • 2021-02-20 01:15

    Because you have set yourTextField.borderStyle = UITextBorderStyle....

    0 讨论(0)
  • 2021-02-20 01:17

    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);
    
    0 讨论(0)
  • 2021-02-20 01:23

    A slight update

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

    0 讨论(0)
  • 2021-02-20 01:25

    Why not just shift the drawing rect and then invoke the super method implementation call? Swift code ensues...

    override func drawPlaceholderInRect(rect: CGRect) {
        var newRect = CGRectInset(rect, 0, 2)
        newRect.origin.y += 2
        super.drawPlaceholderInRect(newRect)
    }
    
    0 讨论(0)
提交回复
热议问题