Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I invoke drawPlaceholderInRect

I have a custom UITextField so that I get get a custom placeholder text color as may answers suggest. However, I also want to change the color of the placeholder text at runtime, so I created a property.

// Overide the placholder text color
- (void) drawPlaceholderInRect:(CGRect)rect
{
    [self.placeholderTextColor setFill];
    [self.placeholder drawInRect:rect
                        withFont:self.font
                   lineBreakMode:UILineBreakModeTailTruncation
                       alignment:self.textAlignment];
}

- (void) setPlaceholderTextColor:(UIColor *)placeholderTextColor
{
    // To verify this is being called and that the placeholder property is set
    NSLog(@"placeholder text: %@", self.placeholder); 

    _placeholderTextColor = placeholderTextColor;
    [self setNeedsDisplay]; // This does not trigger drawPlaceholderInRect
}

The problem is the docs say I should not call drawPlaceholderInRect directly, and [self setNeedsDisplay]; dons't work. Any ideas?

like image 792
Robert Avatar asked Apr 20 '26 23:04

Robert


2 Answers

The drawPlaceholderInRect: method is only called if the textfield actually contains a placeholder string. (it doesn't by default)

Try to set a placeholder string for your textfield in Interface Builder.
Also make sure you set your subclass in the Custom Class field.

Update:
I tried the reproduce the issue described in the question and also ran into that problem. According to this Stack Overflow question this seems to be a common problem: https://stackoverflow.com/a/2581866/100848.

As a workaround (at least when targeting iOS >= 6.0), you could use UITextField's attributedPlaceHolder:

NSMutableAttributedString* attributedString = [[NSMutableAttributedString alloc] initWithString:@"asdf"];
NSDictionary* attributes = @{NSForegroundColorAttributeName:[UIColor redColor]};
[attributedString setAttributes:attributes range:NSMakeRange(0, [attributedString length])];
[self.textField setAttributedPlaceholder:attributedString];
like image 134
Thomas Zoechling Avatar answered Apr 22 '26 13:04

Thomas Zoechling


You can also achieve this by subclassing the UITextField and overriding drawPlaceholderInRect

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

like image 39
Steven Veltema Avatar answered Apr 22 '26 12:04

Steven Veltema