UITextField placeholder string is always Top aligned in ios7

后端 未结 6 2660
滥情空心
滥情空心 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: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/

提交回复
热议问题