繁体   English   中英

如何为两个 UILabel 项目获得相同的字符间距

[英]How to get the same character spacing for two UILabel items

http://home.astound.net/~puzzleblog/uploaded_images/calculator87355.gif

我正在创建一个计算器; 为此,我将字体类型设为“DBLCDTempBlack”。 它工作正常。 在图像中,一个 label 中的数字运行在另一个 label 的顶部,而底部的 label 有文本“888888888”等以获得淡光效果。 因此,要获得这种效果,我们需要将两个标签放在一起。 当我试图连续给顶部 label 提供“1”时,它与后面的数字不对齐。

在这种字体类型中,数字 2-9,0 通常由 8 个组成,因此它们是对齐的。 但是当我给出“1”时,字符间距会受到干扰。

编辑字体,使 1 与其他数字的宽度相同,应该是这样。

我写了一个小方法,使间距正确,以便在其中包含 1。 我不知道这是否是最好的方法,但事实证明,如果你在 1 之前添加一个空格,除非它是字符串中的第一个字符,否则间距是完美的。

- (NSMutableString *)stringForDigitalDisplayFromString:(NSString *)string {

    NSMutableString *formattedString = [[NSMutableString alloc] initWithCapacity:10];

    for (int i = 0; i < [string length]; i++) {
        char character = [string characterAtIndex:i];
        //if we have a 1 and it is not the first digit
        if (character == '1' && i != 0) {
            [formattedString appendString:[NSString stringWithFormat:@" %c",character]];
        }
        else {
            [formattedString appendString:[NSString stringWithFormat: @"%c",character]];
        }
    }

    return formattedString;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM