0

I'm trying to edit an image by adding text to it, but the code I got from this post does not work: How to write text on image in Objective-C (iOS)?

My image-editing code looks like this:

func addTextToImage(image:UIImage, text:NSString, pointof: CGPoint) -> UIImage{

    let font:UIFont = UIFont.boldSystemFontOfSize(12)

    let dict:NSDictionary = [NSFontAttributeName : font]

    UIGraphicsBeginImageContext(image.size)
    image.drawInRect(CGRectMake(0, 0, image.size.width, image.size.height))
    let rect: CGRect = CGRectMake(pointof.x, pointof.y, image.size.width, image.size.height)
    let color: UIColor = UIColor.whiteColor()
    text.drawInRect(CGRectIntegral(rect), withAttributes:dict)

    let newImage:UIImage = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()

    return newImage
}

And then I use the textViewDidChange method to add the text to the image, but it does not work:

func textViewDidChange(textView: UITextView){
    backgroundImage.image = addTextToImage(imageToView, text: textViewer.text, pointof: CGPointMake(0, 0))
}

Any suggestions on what I'm doing wrong would be appreciated.

Community
  • 1
  • 1
martin
  • 1,894
  • 4
  • 37
  • 69
  • First we need to know what is wrong. "Does not work" is not helpful. What result does you code give? Is textViewDidChange being called? Is newImage actually created? – rdelmar Feb 09 '15 at 23:50
  • @rdelmar Nothing happens, textViewDidChange is being called and therefore I assume it's being created. – martin Feb 09 '15 at 23:53
  • Don't assume. Put a print statement in just before "return newImage", and see if newImage is non-nil. – rdelmar Feb 09 '15 at 23:54
  • @rdelmar Okay, I did so, and both functions are being called and the new image created is not empty. – martin Feb 10 '15 at 00:00
  • Your code works for me, but the text is black, not white. If you want white text, you need to add the NSForegroundColorAttributeName key to your attributes dictionary. – rdelmar Feb 10 '15 at 00:09
  • @rdelmar Now it works for me too, I don't know what I did really, now the only problem is the white color, which I now see is not being set anywhere in my code. Do you know how to do this? – martin Feb 10 '15 at 00:18
  • 1
    Yes, i put that in my last comment. – rdelmar Feb 10 '15 at 00:19

1 Answers1

0
func addTextToImage(drawText text: NSString, inImage image: UIImage, atPoint point: CGPoint) -> UIImage
{
    let textColor = UIColor.blue

    let textFont = UIFont(name: "ChalkboardSE-Regular", size: 26)!

    let scale = UIScreen.main.scale
    UIGraphicsBeginImageContextWithOptions(image.size, false, scale)

    let textFontAttributes = [
        NSFontAttributeName: textFont,
        NSForegroundColorAttributeName: textColor,
        ] as [String : 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!
}
appFormation
  • 231
  • 3
  • 13