简体   繁体   English

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

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

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

I am creating a calculator;我正在创建一个计算器; for that I have taken the font type as "DBLCDTempBlack".为此,我将字体类型设为“DBLCDTempBlack”。 It is working properly.它工作正常。 In the image digits in a label are running on top of another label, and the bottom label has text "888888888" and so on to get an effect of faded light.在图像中,一个 label 中的数字运行在另一个 label 的顶部,而底部的 label 有文本“888888888”等以获得淡光效果。 So to get this effect, we need to have two labels on top of one another.因此,要获得这种效果,我们需要将两个标签放在一起。 When I am trying to give "1" continuously to the top label, it is not aligning with the digits which are at the back.当我试图连续给顶部 label 提供“1”时,它与后面的数字不对齐。

Digits 2-9,0 are generally made up of 8 in this font type so they are aligning.在这种字体类型中,数字 2-9,0 通常由 8 个组成,因此它们是对齐的。 But when I am giving "1", character spacing is getting disturbed.但是当我给出“1”时,字符间距会受到干扰。

Edit the font so that 1 is the same width as the other digits, as it should be.编辑字体,使 1 与其他数字的宽度相同,应该是这样。

I wrote a little method that makes the spacing correct for having 1's in there.我写了一个小方法,使间距正确,以便在其中包含 1。 I don't know if this is the best way, but it turns out if you add a space before a 1, unless it's the first character in the string, the spacing is perfect.我不知道这是否是最好的方法,但事实证明,如果你在 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