【发布时间】:2011-12-17 03:54:43
【问题描述】:
我正在使用 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 是否正在生成错误。
知道这里的问题是什么,或者是否有其他方法来设置字体?
已编辑
我刚刚意识到 iPad 运行的是 iOS 4(我相信这在 iOS >= 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];
}
【问题讨论】:
-
在进入
dictionaryWithObject方法之前检查以确保font不为零。 -
添加了一个 if(font != nil) 只是为了确定。没有喜悦。还有其他想法吗?
-
不,还没有……在我看来,您似乎在做正确的事。尝试以不同的方式创建字典...例如:
dictionaryWithObjects:andKeys:或alloc+init(如果您不使用 ARC,请确保稍后发布)。看看崩溃是否仍然发生。
标签: iphone ios ipad uisegmentedcontrol