How to get the same character spacing for two UILabel items
I am creating a calculator; for that I have taken the font type as "DBLCDTempBlack". It is working properly. In the image digits in a label are running on top of a开发者_高级运维nother label, and the bottom label has text "888888888" and so on to get an effect of faded light. 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.
Digits 2-9,0 are generally made up of 8 in this font type so they are aligning. But when I am giving "1", character spacing is getting disturbed.
Edit the font so that 1 is the same width as the other digits, as it should be.
I wrote a little method that makes the spacing correct for having 1's in there. 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.
- (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;
}
精彩评论