【发布时间】:2014-07-24 05:14:24
【问题描述】:
我有一个UIViewController,我在它的viewDidLoad: 中以编程方式创建了一个UILabel,如下所示:
- (void)viewDidLoad
{
UILabel *navTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(17, 0, 100, 100)];
navTitleLabel.backgroundColor = [UIColor clearColor];
navTitleLabel.textAlignment = NSTextAlignmentCenter;
navTitleLabel.textColor=[UIColor colorWithHexString:@"#000029"];
[navTitleLabel setFont:[UIFont fontWithName:@"Pennyscript" size:20]];
self.navigationItem.titleView = navTitleLabel;
}
我在ViewController's 身份检查器选项卡中设置了“用户定义的运行时属性”:
我在这里尝试以编程方式将UILabel navTitleLabel 的字体设置为我的自定义字体,然后将UILabel 添加到navigationBar's titleView。我究竟做错了什么?我怎样才能实现这个目标?
使用 VC ID-inspector 中的 UDKA(User defined key attribute),应用程序崩溃并显示以下堆栈跟踪消息:
由于未捕获的异常“NSUnknownKeyException”而终止应用程序,原因:“[setValue:forUndefinedKey:]:此类与键 fontName 的键值编码不兼容。”
当我从 ViewController 中删除我的 用户定义的键属性时,我可以导航到 VC 而不会崩溃。
【问题讨论】:
-
您是否在 Info Plist 文件中将字体添加到您的项目中?以及对应的.ttf-文件?
-
是的。我已经这样做了。我已经在情节提要中使用了 UILabel 上的字体。但是这个 UILabel 是程序化的,所以我很困惑。
-
hmm,通常你不必定义 用户定义属性 然后......就像你一样在代码中使用字体就足够了。
-
您确定 Pennyscript 是正确的字体名称吗?您可以使用以下问题的答案检查自定义字体的正确字体名称:stackoverflow.com/questions/15984937/…
-
用户定义运行时属性和
[navTitleLabel setValue:@"NavBar Title" forKeyPath:@"fontName"]这行是多余的,顺便设置一下标签的字体就够了。
标签: ios objective-c uinavigationbar