我正在使用setTitleTextAttributes更改UISegmentedControl
上的字体,如下所示。
UIFont *font = [UIFont boldSystemFontOfSize:14.0f];
NSDictionary *attributes = [NSDictionary dictionaryWithObject:font forKey:UITextAttributeFont];
[self setTitleTextAttributes:attributes forState:UIControlStateNormal];</pre></code>
这在模拟器中工作正常,但在iPad上运行时崩溃了。在创建字典时发生了错误的访问错误,我使用NSLog语句来验证UITextAttributeFont是否正在生成错误。
任何想法是什么问题在这里或是否有另一种方法来设置字体?
EDITED
我刚刚意识到iPad正在运行iOS 4(我相信这支持iOS&gt; = 5)。测试版本支持的最佳方法是什么?
根据Dirty Henry的建议,正确实施如下。
if ([self respondsToSelector:@selector(setTitleTextAttributes: forState:)]) {
UIFont *font = [UIFont boldSystemFontOfSize:14.0f];
NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys: font, UITextAttributeFont, nil];
[self setTitleTextAttributes:attributes forState:UIControlStateNormal];
}
答案 0 :(得分:4)
您的iPad是否运行iOS 5.0或更高版本? (此版本中引入了UITextAttributeFont)。 (我认为你的代码没有任何问题)