Last active
October 19, 2020 18:49
-
-
Save leptos-null/27de71b8db7db7d52bcd441acf462a47 to your computer and use it in GitHub Desktop.
Draw UIImage of Unicode character
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
@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