Skip to content

Instantly share code, notes, and snippets.

@leptos-null
Last active October 19, 2020 18:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leptos-null/27de71b8db7db7d52bcd441acf462a47 to your computer and use it in GitHub Desktop.
Save leptos-null/27de71b8db7db7d52bcd441acf462a47 to your computer and use it in GitHub Desktop.
Draw UIImage of Unicode character
@implementation UIImage (UIImageUnicode)
+ (UIImage *)imageFromUnicodePoint:(unichar)codepoint compatibleWithTraitCollection:(UITraitCollection *)traitCollection API_AVAILABLE(ios(10.0)) {
NSString *str = [NSString stringWithCharacters:&codepoint length:1];
UIFont *weightRef = [UIFont preferredFontForTextStyle:UIFontTextStyleBody compatibleWithTraitCollection:traitCollection];
UIFont *sizeRef = [UIFont preferredFontForTextStyle:UIFontTextStyleTitle2 compatibleWithTraitCollection:traitCollection];
NSAttributedString *drawReady = [[NSAttributedString alloc] initWithString:str attributes:@{
NSFontAttributeName : [weightRef fontWithSize:sizeRef.pointSize]
}];
UIGraphicsBeginImageContextWithOptions([drawReady size], NO, 0);
[drawReady drawAtPoint:CGPointZero];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return [image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment