如何让文字显示在图像的中心?

时间:2015-06-03 19:36:56

标签: swift ios8 uiimageview uiimage uigraphicscontext

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

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。但是我没有得到如何改变它以便它在所有情况下看起来都很好。请帮我解决这个问题。

3 个答案:

答案 0 :(得分:3)

您可以将文字置于图像顶部附近,如下所示:

    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
}

答案 1 :(得分:0)

这可能很简单,因为你建议使用"我想将文本对齐设置为居中"。

在这里添加一行:

// 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)并将文本矩形设置为与图像一样大,则应该能够考虑Rect的宽度,从而相应地居中所有文本。 / p>

答案 2 :(得分:0)

添加到@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