如何让文字出现在图片的中央?

How to make text appear in centre of the image?

我想在图片上添加文字,我可以按照下面的代码所示进行操作。

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
}

我调用了这个函数,如下所示

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)

但问题是,文本被用户拿走了。他们可以给出单个单词或大句子。因此,如果他们只给出一个词,它应该出现在图像的顶部和中间,或者从左到右出现在顶部。更像是,我想将文本对齐方式设置为居中。为此,我应该更改上面给出的 CGPoint。但我不知道如何更改它,以便它在所有情况下看起来都不错。请帮助我如何实现这一目标。

它可能像您在 "I want to set the text alignment to centre" 中建议的那样简单。

在此处添加一行:

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

并添加到您的数组中:

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

这应该使文本在图像中居中。

如果将文本原点改回 (0, 0) 并将文本矩形设置为与图像一样大,它应该能够考虑矩形的宽度,从而使所有文本相应地居中.

您可以像这样将文本居中靠近图像顶部:

    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)
    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)

    var newImage: UIImage = UIGraphicsGetImageFromCurrentImageContext()

    UIGraphicsEndImageContext()
    return newImage
}

已添加到@osteven 的回答中。下面的代码按我的预期工作。希望对其他人有帮助。

 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