0

I'm trying to format the placeholder text of a UITextField using an NSAttributedString. This code works successfully for the foreground color and kerning attributes. However, it won't change the font or the font size. What am I doing wrong? Thanks!

NSAttributedString *aString = [[NSAttributedString alloc] initWithString:
                                  @"USER NAME"
                                  attributes:@{
                                              NSForegroundColorAttributeName: [UIColor redColor],
                                              NSFontAttributeName : [UIFont fontWithName:@"Georgia" size:8.0f],
                                              NSKernAttributeName : [NSNumber numberWithFloat:3.0f]
                                                                                                    }];
self.userNameTextField.attributedPlaceholder = [aString copy];
AndroidDev
  • 20,466
  • 42
  • 148
  • 239

2 Answers2

0

It looks like the placeholder font size is governed by the UITextField's font so I think you will have to subclass UITextField and add:

-(void)drawPlaceholderInRect:(CGRect)rect
{
    NSString *aString = @"USER NAME";
    [aString drawInRect:rect withAttributes:@{NSForegroundColorAttributeName: [UIColor redColor], NSFontAttributeName : [UIFont fontWithName:@"Georgia" size:8.0f], NSKernAttributeName : [NSNumber numberWithFloat:3.0f]}];
}
Gareth
  • 2,051
  • 2
  • 19
  • 14
0

For me it works when I set custom font for UITextField, and then set other attributes for placeholder. My example that works:

_searchField.font = [UIFont fontWithName:fontRokkitRegular size:20];
UIColor *color = [UIColor colorWithRed:150.0f/255 green:150.0f/255 blue:150.0f/255 alpha:1];
_searchField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"Keyword search" attributes:@{NSForegroundColorAttributeName:color}];