Created
July 25, 2021 16:57
-
-
Save iUsmanN/629f60ac7aa5366a572e64ed2c855085 to your computer and use it in GitHub Desktop.
Swift (Text On Image)
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
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