Skip to content

Instantly share code, notes, and snippets.

@joninsky
Created October 18, 2018 04:24
Show Gist options
  • Save joninsky/6ed93321794101738a22845515481964 to your computer and use it in GitHub Desktop.
Save joninsky/6ed93321794101738a22845515481964 to your computer and use it in GitHub Desktop.
Function that will take a unicode character from the Linear Icon set and turn it into a `UIImage`
/// Function that will take a unicode character from the Linear Icon set and turn it into a `UIImage`
///
/// - Parameters:
/// - unicode: The unicode character (Icon) to turn into an image
/// - color: The color you want the Icon to be
/// - size: The size you want the Icon to be
/// - Returns: Gives you a crisp `UIImage`
public func image(fromUnicode unicode: String, color: UIColor, size: CGFloat) -> UIImage {
let attributes: [NSAttributedString.Key: Any] = [NSAttributedString.Key.font: self.iconFont(size), NSAttributedString.Key.backgroundColor: UIColor.clear, NSAttributedString.Key.foregroundColor: color]
let S = CGSize(width: size, height: size)
let R = CGRect(x: 0, y: 0, width: size, height: size)
UIGraphicsBeginImageContextWithOptions(S, false, 0)
unicode.draw(in: R, withAttributes: attributes)
guard let image = UIGraphicsGetImageFromCurrentImageContext() else {
return UIImage()
}
UIGraphicsEndImageContext()
return image
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment