Google+ Followers

2014年7月6日 星期日

iOS 開發筆記 - 列出系統內建 monospaced font (等寬字型/等寬字體)

列出系統所有字型:

for (int i=0 ; i< [[UIFont familyNames] count]; ++i) {
        NSLog(@"Font Family: %@", [UIFont familyNames][i]);
        for (int j=0; j<[[UIFont fontNamesForFamilyName:[UIFont familyNames][i]] count] ; ++j) {
            NSLog(@"Font Name: %@", [UIFont fontNamesForFamilyName:[UIFont familyNames][i]][j]);
        }
}


找尋 monospaced font:

NSString *words = @"Hello World!";
for (int i=0 ; i< [[UIFont familyNames] count]; ++i) {
    //NSLog(@"Font Family: %@", [UIFont familyNames][i]);
    for (int j=0; j<[[UIFont fontNamesForFamilyName:[UIFont familyNames][i]] count] ; ++j) {
        //NSLog(@"Font Name: %@", [UIFont fontNamesForFamilyName:[UIFont familyNames][i]][j]);
     
        float width = -1 ;
        bool found = true;
        for (int k=0; k< [words length]; ++k) {
            unichar word = [words characterAtIndex:k];
            CGSize wordSize = [[NSString stringWithFormat:@"%C", word] sizeWithAttributes:@{
                NSFontAttributeName: [UIFont fontWithName:[UIFont fontNamesForFamilyName:[UIFont familyNames][i]][j] size:16]
            }];
            if (width < 0) {
                width = wordSize.width;
            } else if (width != wordSize.width) {
                found = false;
                break;
            }
        }
        if (found) {
            NSLog(@"width(size16): %f, monospaced font: %@", width, [UIFont fontNamesForFamilyName:[UIFont familyNames][i]][j]);
        }
    }
}


結果:

width(size16): 9.601562, monospaced font: CourierNewPS-BoldMT
width(size16): 9.601562, monospaced font: CourierNewPS-ItalicMT
width(size16): 9.601562, monospaced font: CourierNewPSMT
width(size16): 9.601562, monospaced font: CourierNewPS-BoldItalicMT
width(size16): 9.601562, monospaced font: Courier-BoldOblique
width(size16): 9.601562, monospaced font: Courier
width(size16): 9.601562, monospaced font: Courier-Bold
width(size16): 9.601562, monospaced font: Courier-Oblique
width(size16): 9.632812, monospaced font: Menlo-Italic
width(size16): 9.632812, monospaced font: Menlo-Bold
width(size16): 9.632812, monospaced font: Menlo-Regular
width(size16): 9.632812, monospaced font: Menlo-BoldItalic

沒有留言:

張貼留言