Skip to content

Instantly share code, notes, and snippets.

@dclelland
Last active August 29, 2015 14:04
Show Gist options
  • Save dclelland/ad45371ef69789eb1003 to your computer and use it in GitHub Desktop.
Save dclelland/ad45371ef69789eb1003 to your computer and use it in GitHub Desktop.
Check whether a character is renderable on iOS
/* The character we are going to check*/
NSString *character = @"字";
/* Turn it into a character set ahead of our comparison */
NSCharacterSet *characterSet = [NSCharacterSet characterSetWithCharactersInString:character];
/* Fetch the system fallback font, as [UIFont familyNames] won't include it */
UIFontDescriptor *fallbackFontDescriptor = [UIFont fontWithName:@".PhoneFallBack" size:12.0f].fontDescriptor;
/* Start with the fallback font's character set */
NSMutableCharacterSet *renderableCharacterSet = [[fallbackFontDescriptor objectForKey:UIFontDescriptorCharacterSetAttribute] mutableCopy];
/* Create a union of all character sets. You'll want to cache this as it's too slow to be called every time you do this */
for (NSString *familyName in [UIFont familyNames]) {
for (NSString *fontName in [UIFont fontNamesForFamilyName:familyName]) {
UIFontDescriptor *fontDescriptor = [UIFont fontWithName:fontName size:12.0f].fontDescriptor;
[renderableCharacterSet formUnionWithCharacterSet:[fontDescriptor objectForKey:UIFontDescriptorCharacterSetAttribute]];
}
}
/* Finally, make the comparison */
NSLog(@"%@", [renderableCharacterSet isSupersetOfSet:characterSet] ? @"character is renderable" : @"character is not renderable");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment