Created
February 25, 2015 18:47
-
-
Save alejandro-isaza/3f29c6da342f639b9232 to your computer and use it in GitHub Desktop.
Code to generate a table of character widths for a particular font. Note that this does not account for things like kerning. Use the generated table to get a good estimate of a string size without having to invoke UIKit.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma GCC diagnostic push | |
#pragma GCC diagnostic ignored "-Wdeprecated-declarations" | |
UIFont* font = [UIFont fontWithName:@"Baskerville-BoldItalic" size:26]; | |
NSString* latinString = @"W"; | |
CGSize latinSize = [latinString sizeWithFont:font]; | |
NSString* kanjiString = @"儸"; | |
CGSize kanjiSize = [kanjiString sizeWithFont:font]; | |
printf("static const int kCharacterHeight = %.0f;\n", MAX(latinSize.height, kanjiSize.height)); | |
printf("static const int kCharacterDefaultWidth = %.0f;\n", MAX(latinSize.width, kanjiSize.width)); | |
printf("static const int kCharacterWidths[256] = {\n"); | |
for (unichar c = 0; c < 256; c+= 1) { | |
NSString* s = [NSString stringWithCharacters:&c length:1]; | |
printf("%2.0f, ", [s sizeWithFont:font].width); | |
if ((c + 1) % 16 == 0) | |
printf("\n"); | |
} | |
printf("};\n"); | |
#pragma GCC diagnostic pop |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment