From 1de69562f912b80dd988416be71bac1eb900350c Mon Sep 17 00:00:00 2001 From: Vincent Sam Date: Tue, 24 Jul 2018 14:25:11 -0400 Subject: [PATCH] [GH-1440] - Added function to insert a text in the current image. --- MapboxNavigation/UIImage.swift | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/MapboxNavigation/UIImage.swift b/MapboxNavigation/UIImage.swift index 876c277f05..00943868b4 100644 --- a/MapboxNavigation/UIImage.swift +++ b/MapboxNavigation/UIImage.swift @@ -9,4 +9,22 @@ extension UIImage { UIGraphicsEndImageContext() return tintedImage } + + func insert(text: NSString, color: UIColor, font: UIFont, atPoint: CGPoint, scale: CGFloat) -> UIImage? { + UIGraphicsBeginImageContextWithOptions(size, false, scale) + + let textStyle = NSMutableParagraphStyle() + textStyle.alignment = .center + + let textFontAttributes: [NSAttributedStringKey: Any] = [.font: font, .foregroundColor: color, .paragraphStyle: textStyle] + draw(in: CGRect(x: 0, y: 0, width: size.width, height: size.height)) + + let rect = CGRect(x: atPoint.x, y: atPoint.y, width: size.width, height: size.height) + text.draw(in: rect.integral, withAttributes: textFontAttributes) + + let newImage = UIGraphicsGetImageFromCurrentImageContext() + UIGraphicsEndImageContext() + + return newImage + } }