代码之家  ›  专栏  ›  技术社区  ›  luvieere

iPhone应用程序文本徽章长度

  •  0
  • luvieere  · 技术社区  · 15 年前

    我正在尝试用未记录的setapplicationbadgestring方法在我的iPhone应用程序上添加一个文本徽章。我遇到的问题是如何正确地计算字符数。有时我可以适应字符,有时它会用点替换我的部分字符串。我也在uilabel上看到过同样的行为,如果文本不合适,它会被“…”切断。在文本标记的特定情况下,我如何以编程方式找出文本何时会被截断,因为我无法检查其大小-它实际上似乎不是常量。谢谢您!

    1 回复  |  直到 15 年前
        1
  •  1
  •   probablyCorey    15 年前

    字符数因字符宽度不同而不同。一 W 比一个L*长,所以一组W比一组L更快被截断。

    通过尝试和错误,您可以使用 非字符串 方法 -(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];