ofxiOSKeyboard placeholder font colour changed in iOS 7

Just sharing a problem (and a fix) that I came across yesterday…

Apple, in their infinite wisdom, changed the colour used for placeholder text in the UITextField class in iOS 7. And as OF uses UITextField in its ofxiOSKeyboard class, then we see this change appear in OF too. For my app, this caused legibility problems on a dark backgrounds.

To change the colour, I made a small addition in ofxiOSKeyboard.mm:

Original method:

- (void) setPlaceholder: (NSString *)text
{
	[_textField setPlaceholder:text];
}

My update:

- (void) setPlaceholder: (NSString *)text
{
	[_textField setPlaceholder:text];
    
    // Set the placeholder text colour correctly for iOS 6 & iOS 7
    if ([_textField respondsToSelector:@selector(setAttributedPlaceholder:)]) {
        UIColor *color = [UIColor lightTextColor];
        NSAttributedString * subString = [[NSAttributedString alloc] initWithString:text attributes:@{NSForegroundColorAttributeName: color}];
        _textField.attributedPlaceholder = subString;
        [subString release];
    }
    else {
        // do nothing, as the colour should be ok in iOS 5
    }
}

Works for me - hope that helps someone…