Ios NSAttribute字符串不会更改字体
我正在尝试使用NSAttribute字符串格式化UITextField的占位符文本。这段代码成功地用于前景色和紧排属性。但是,它不会更改字体或字体大小。我做错了什么?谢谢Ios NSAttribute字符串不会更改字体,ios,objective-c,nsattributedstring,Ios,Objective C,Nsattributedstring,我正在尝试使用NSAttribute字符串格式化UITextField的占位符文本。这段代码成功地用于前景色和紧排属性。但是,它不会更改字体或字体大小。我做错了什么?谢谢 NSAttributedString *aString = [[NSAttributedString alloc] initWithString: @"USER NAME" attribute
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];
占位符字体大小似乎由UITextField的字体控制,因此我认为您必须将UITextField子类化并添加:
-(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]}];
}
对我来说,当我为UITextField设置自定义字体,然后为占位符设置其他属性时,它就会工作。我的例子是:
_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}];