我已经为我的UIPickerView追了几个小时了。我已经把它缩小到一个委托方法,但老实说无法搞清楚。所有它应该做的是获取iPhone本机上的字体系列列表,并在它们所代表的字体中在选择器视图中显示它们的标题。
-(NSAttributedString *)pickerView:(UIPickerView *)pickerView attributedTitleForRow:(NSInteger)row forComponent:(NSInteger)component
{
NSLog(@"attributedTitleForRow");
NSString *fontName = [self.fontFamilies objectAtIndex:row];
NSLog(@"Font: %@", fontName);
NSDictionary *attributes = @{NSFontAttributeName:fontName};
NSLog(@"Attributes: %@", attributes);
NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString:fontName attributes:attributes];
NSLog(@"Returning %@", attributedString);
return attributedString;
}
打印输出,包括例外:
2013-07-31 23:28:42.875 [4689:907] attributedTitleForRow
2013-07-31 23:28:42.876 [4689:907] Font: Thonburi
2013-07-31 23:28:42.879 [4689:907] Attributes: {
NSFont = Thonburi;
}
2013-07-31 23:28:42.881 [4689:907] Returning Thonburi{
NSFont = Thonburi;
}
2013-07-31 23:28:42.884 [4689:907] attributedTitleForRow
2013-07-31 23:28:42.886 [4689:907] Font: Snell Roundhand
2013-07-31 23:28:42.887 [4689:907] Attributes: {
NSFont = "Snell Roundhand";
}
2013-07-31 23:28:42.889 [4689:907] Returning Snell Roundhand{
NSFont = "Snell Roundhand";
}
2013-07-31 23:28:42.892 [4689:907] attributedTitleForRow
2013-07-31 23:28:42.894 [4689:907] Font: Academy Engraved LET
2013-07-31 23:28:42.896 [4689:907] Attributes: {
NSFont = "Academy Engraved LET";
}
2013-07-31 23:28:42.898 [4689:907] Returning Academy Engraved LET{
NSFont = "Academy Engraved LET";
}
2013-07-31 23:28:42.920 [4689:907] -[__NSCFConstantString screenFontWithRenderingMode:]: unrecognized selector sent to instance 0x3977f364
2013-07-31 23:28:42.922 [4689:907] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFConstantString screenFontWithRenderingMode:]: unrecognized selector sent to instance 0x3977f364'
答案 0 :(得分:1)
NSFontAttributeName
具有误导性名称。期望值不是字体名称,而是UIFont
实例。您需要根据所获得的字体名称更新代码以创建UIFont
对象,并在UIFont
字典中使用attributes
。
阅读NSFontAttributeName
的文档。它告诉您需要UIFont
个对象,而不是NSString
。