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

标签 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。但我不知道如何更改它,以便它在所有情况下看起来都不错。请帮助我如何实现这一目标。

最佳答案

您可以像这样将文本居中放置在图像顶部附近:

    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
}

关于swift - 如何让文字出现在图片的中央?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30629118/

相关文章:

ios - 如何设置堆栈背景颜色?

ios - 为什么我的弹出窗口在 iPhone 6 Plus 上的 iOS 8.1 中崩溃? iOS 8 可以工作(实际上是 Xcode 6.0 到 6.1 的 bug)

objective-c - 根据图像大小调整自定义 UITableViewCell 和包含的 UIImageView

ios - UINavigationBar 的 UIAppearance 在与自定义 View Controller 一起使用时无法正常工作...?

swift - 如何将 Unix 时间戳转换为 Swift NSDate 对象?

ios - 登录到 Facebook 评论 iOS (swift) 没有响应

ios8 - 使用 xib 而不是 Storyboard的 iOS 应用扩展

swift - 如何快速暂停和恢复 NSTimer.scheduledTimerWithTimeInterval?

ios - 应用程序在按钮上崩溃 Click Action iOS

ios - 我应该调整图像大小以在 UITableView 中显示吗?