Iphone app文本徽章长度

时间:2009-09-27 13:57:14

标签: iphone iphone-sdk-3.0

我正在尝试使用未记录的setApplicationBadgeString方法在我的Iphone应用上放置文本徽章。我遇到的问题是获得正确的字符数。有时我可以适应角色,有时它会用点替换我的部分字符串。我在UILabel上也看到过相同的行为,如果文本不合适,它会被“......”切断。我如何能够以编程方式找出在文本徽章的特定情况下文本何时被截断,因为我无法检查其大小 - 它实际上似乎不是恒定的。谢谢!

1 个答案:

答案 0 :(得分:1)

字符数量因字符宽度的不同而有所不同。 W 比* l **长,因此一堆W将比一堆l更早被截断。

通过反复试验,您可以使用 NSString 的方法 - (CGSize)sizeWithFont:(UIFont *)font 来查找徽章上允许的最大宽度。您必须弄清楚徽章使用的字体大小。你的代码应该最终看起来像这样......

// I'm not sure what the badge font size is, you will have to test for this
UIFont *font = [UIFont systemFontOfSize:12];

// Keep adding 1's to the badge string until you get the ...'s, then you will 
// know the maximum size of the badge string!
NSString *badgeString = @"111";     
NSLog(@"width: %f", [badgeString sizeWithFont:font].width);

[[UIApplication sharedApplication] setApplicationIconBadgeNumber:badgeString];
相关问题