Skip to content

Instantly share code, notes, and snippets.

@iUsmanN
Created July 25, 2021 16:57
Show Gist options
  • Save iUsmanN/629f60ac7aa5366a572e64ed2c855085 to your computer and use it in GitHub Desktop.
Save iUsmanN/629f60ac7aa5366a572e64ed2c855085 to your computer and use it in GitHub Desktop.
Swift (Text On Image)
func textOnImage(drawText text: String, inImage image: UIImage, atPoint point: CGPoint) -> UIImage {
let textColor = UIColor.white
let textFont = UIFont(name: "Helvetica Bold", size: 20)!
let scale = UIScreen.main.scale
UIGraphicsBeginImageContextWithOptions(image.size, false, scale)
let textFontAttributes = [
NSAttributedString.Key.font: textFont,
NSAttributedString.Key.foregroundColor: textColor,
] as [NSAttributedString.Key : Any]
image.draw(in: CGRect(origin: CGPoint.zero, size: image.size))
let rect = CGRect(origin: point, size: image.size)
text.draw(in: rect, withAttributes: textFontAttributes)
let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return newImage!
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment