Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
@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
You can’t perform that action at this time.