iOS: Monospaced fonts aren't?
iOS: Monospaced fonts aren't?
- Subject: iOS: Monospaced fonts aren't?
- From: Phillip Mills <email@hidden>
- Date: Fri, 10 Dec 2010 14:39:26 -0500
I've been having one of those "I must be doing something stupid" days. The code I'm trying to write needs to pad one string with spaces so that certain characters line up visually with selected characters in a different string (within a view). It seemed like a relatively easy task as long as I could require the use of a monospaced font. However, it comes out looking various kinds of wrong depending on how many spaces I add. I've tried this with a number of Courier variants with the same result.
The following test seems to confirm that a string of spaces does not render in the same bounding box as a string of alphanumeric characters or punctuation marks. (Also, that there may not be a neat and obvious formula for how to compensate.) Are these results reproducible? Was a fixed-size character width a bad assumption? Or is it a bug?
- (void)debugFont {
UIFont *font = [UIFont fontWithName:@"CourierNewPS-BoldMT" size:24];
// With 22 characters, there's a width difference of 9
NSString *s1 = @"1234567890123456789012";
NSString *s2 = @" ";
// The choice of non-space character doesn't seem to matter
NSString *s5 = @"......................";
// With 23 characters, there's a width difference of 10
NSString *s3 = @"12345678901234567890123";
NSString *s4 = @" ";
NSLog(@".%@. size: %f", s1, [s1 sizeWithFont:font].width);
NSLog(@".%@. size: %f", s2, [s2 sizeWithFont:font].width);
NSLog(@".%@. size: %f", s5, [s5 sizeWithFont:font].width);
NSLog(@".%@. size: %f", s3, [s3 sizeWithFont:font].width);
NSLog(@".%@. size: %f", s4, [s4 sizeWithFont:font].width);
}
2010-12-10 14:19:03.936 FontTest[1834:207] .1234567890123456789012. size: 317.000000
2010-12-10 14:19:03.937 FontTest[1834:207] . . size: 308.000000
2010-12-10 14:19:03.938 FontTest[1834:207] ........................ size: 317.000000
2010-12-10 14:19:03.938 FontTest[1834:207] .12345678901234567890123. size: 332.000000
2010-12-10 14:19:03.939 FontTest[1834:207] . . size: 322.000000
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden