Search code examples
swiftios8uiimageviewuiimageuigraphicscontext

How to make text appear in centre of the image?


I want to add a text on an image, and I'm able to do it as shown in below code.

func textToImage(drawText: NSString, inImage: UIImage, atPoint:CGPoint)->UIImage{

    // Setup the font specific variables
    var textColor: UIColor = UIColor.redColor()
    var textFont: UIFont =  UIFont(name: "AmericanTypewriter", size: 75)!
    let textStyle  = NSTextEffectLetterpressStyle

    //Setup the image context using the passed image.
    UIGraphicsBeginImageContext(inImage.size)

    let textFontAttributes = [
        NSFontAttributeName: textFont,
        NSForegroundColorAttributeName: textColor,
        NSTextEffectAttributeName : textStyle
    ]

    inImage.drawInRect(CGRectMake(0, 0, inImage.size.width, inImage.size.height))

    var rect: CGRect = CGRectMake(atPoint.x, atPoint.y, inImage.size.width, inImage.size.height)

    drawText.drawInRect(rect, withAttributes: textFontAttributes)

    var newImage: UIImage = UIGraphicsGetImageFromCurrentImageContext()

    UIGraphicsEndImageContext()
    return newImage
}

I called this function like shown below

let newImage : UIImage = textToImage(textToAdd!, inImage: imageToSave, atPoint:CGPoint(x: 20, y: 20)) //For now text is displaying at the top in left end for point(x:20 ,y:20)

But the problem is, text is taken by user. They can give either single word or big sentence. So if they give only single word, it should appear in top and middle of the image or else in top from left to right. it is more like, I want to set the text alignment to centre. To achieve it, Accordingly I should change the CGPoint given above. But I'm not getting how to change it so that it should look good in all scenarios. Please help me how to achieve this.


Solution

  • Added to @osteven answer. Below code works as I expected. Hope it helps others.

     func textToImage(drawText: NSString, inImage: UIImage, atPoint:CGPoint)->UIImage{
    
    // Setup the font specific variables
    var textColor: UIColor = UIColor.redColor()
    var textFont: UIFont =  UIFont(name: "AmericanTypewriter", size: 75)!
    let textStyle  = NSTextEffectLetterpressStyle
    
    //Setup the image context using the passed image.
    UIGraphicsBeginImageContext(inImage.size)
    
    let textFontAttributes = [
        NSFontAttributeName: textFont,
        NSForegroundColorAttributeName: textColor,
        NSTextEffectAttributeName : textStyle
    ]
    
    inImage.drawInRect(CGRectMake(0, 0, inImage.size.width, inImage.size.height))
    
     let textSize = drawText.sizeWithAttributes(textFontAttributes)
        if textSize.width < inImage.size.width {
            println("lesser")
            let textRect = CGRectMake(inImage.size.width / 2 - textSize.width / 2, 0,
            inImage.size.width / 2 + textSize.width / 2, inImage.size.height - textSize.height)
            drawText.drawInRect(textRect, withAttributes: textFontAttributes)
        }
        else {
            println("greater")
            let textRect = CGRectMake(0 , 0,
                inImage.size.width, inImage.size.height)
            drawText.drawInRect(textRect, withAttributes: textFontAttributes)
    
        }
    
    var newImage: UIImage = UIGraphicsGetImageFromCurrentImageContext()
    
    UIGraphicsEndImageContext()
    return newImage