Search code examples
iosobjective-cnsattributedstring

NSAttributed String Won't change font


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];

Solution

  • 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}];