Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/101.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios NSAttribute字符串不会更改字体_Ios_Objective C_Nsattributedstring - Fatal编程技术网

Ios NSAttribute字符串不会更改字体

Ios NSAttribute字符串不会更改字体,ios,objective-c,nsattributedstring,Ios,Objective C,Nsattributedstring,我正在尝试使用NSAttribute字符串格式化UITextField的占位符文本。这段代码成功地用于前景色和紧排属性。但是,它不会更改字体或字体大小。我做错了什么?谢谢 NSAttributedString *aString = [[NSAttributedString alloc] initWithString: @"USER NAME" attribute

我正在尝试使用NSAttribute字符串格式化UITextField的占位符文本。这段代码成功地用于前景色和紧排属性。但是,它不会更改字体或字体大小。我做错了什么?谢谢

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