簡體   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