由於字符的寬度不同,字符的數量會有所不同。 a W比* l **更長,所以一堆W將比一堆l更快地被截斷。
通過試驗和錯誤,你可以使用的NSString的方法- (CGSize)sizeWithFont:(UIFont *)字體找出允許在徽章的最大寬度。你將不得不弄清徽章使用的字體大小。你的代碼應該會看起來像這樣...
// 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];