0

I've been chasing an exception for my UIPickerView for hours now. I've narrowed it down to one of the delegate methods, but honestly can't figure it out. All it's supposed to do is take the list of font families that comes natively on the iPhone and display their title in the picker view, in the font they represent.

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

The Printout, incl. Exception:

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'
4

1 回答 1

1

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

阅读NSFontAttributeName. 它告诉你你需要一个UIFont对象,而不是一个NSString.

于 2013-08-01T03:42:29.237 回答