UIPickerView异常

时间:2013-08-01 03:34:49

标签: cocoa-touch nsattributedstring

我已经为我的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'

1 个答案:

答案 0 :(得分:1)

NSFontAttributeName具有误导性名称。期望值不是字体名称,而是UIFont实例。您需要根据所获得的字体名称更新代码以创建UIFont对象,并在UIFont字典中使用attributes

阅读NSFontAttributeName的文档。它告诉您需要UIFont个对象,而不是NSString