Skip to content

Commit

Permalink
[GH-1440] - Added function to insert a text in the current image.
Browse files Browse the repository at this point in the history
  • Loading branch information
vincethecoder committed Jul 24, 2018
1 parent d8ec835 commit 1de6956
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions MapboxNavigation/UIImage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

0 comments on commit 1de6956

Please sign in to comment.