如何在中心绘制文字到uiimage?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何在中心绘制文字到uiimage?相关的知识,希望对你有一定的参考价值。

我想在UIimage中将我的文本设置为中心。但它不在中心。

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

    let textColor = UIColor.white
    let textFont = UIFont(name: "Helvetica Bold", size: 32)!

    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)
    let rect = CGRect(origin: CGPoint(x:(image.size.width/2), y: (image.size.height/2)), size: image.size)

    text.draw(in: rect, withAttributes: textFontAttributes)

    let newImage = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()

    return newImage!
}

来电功能

 imageViewSoldStatus.image = textToImage(drawText: "Sold out", inImage: UIImage(named:"soldout.png")!, atPoint: CGPoint(x: 5.0, y: 5.0))

我花了很长时间来解决这个问题,但我找不到任何解决方案。

红旗我的形象

以上是我获得的屏幕。下面是我想要实现的设计。

更新

Marie Dm回答过使用过!它是文本对齐中心,但不是图像中心

enter image description here

答案

试试这个:

func textToImage(drawText text: NSString, inImage image: UIImage) -> UIImage {
    //draw image first
    UIGraphicsBeginImageContext(image.size)
    image.draw(in: CGRect(x: 0, y: 0, width: image.size.width, height: image.size.height))

    //text attributes
    let font=UIFont(name: "Helvetica-Bold", size: 32)!
    let text_style=NSMutableParagraphStyle()
    text_style.alignment=NSTextAlignment.center
    let text_color=UIColor.white
    let attributes=[NSFontAttributeName:font, NSParagraphStyleAttributeName:text_style, NSForegroundColorAttributeName:text_color]

    //vertically center (depending on font)
    let text_h=font.lineHeight
    let text_y=(image.size.height-text_h)/2
    let text_rect=CGRect(x: 0, y: text_y, width: image.size.width, height: text_h)
    text.draw(in: text_rect.integral, withAttributes: attributes)
    let result=UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
}
另一答案

Marie的回答更新为swift 4

func textToImage(drawText text: NSString, inImage image: UIImage) -> UIImage {
        UIGraphicsBeginImageContext(image.size)
        image.draw(in: CGRect(x: 0, y: 0, width: image.size.width, height: image.size.height))
        let font=UIFont(name: "Helvetica-Bold", size: 14)!
        let text_style=NSMutableParagraphStyle()
        text_style.alignment=NSTextAlignment.center
        let text_color=UIColor.white
        let attributes=[NSAttributedStringKey.font:font, NSAttributedStringKey.paragraphStyle:text_style, NSAttributedStringKey.foregroundColor:text_color]
        let text_h=font.lineHeight
        let text_y=(image.size.height-text_h)/2
        let text_rect=CGRect(x: 0, y: text_y, width: image.size.width, height: text_h)
        text.draw(in: text_rect.integral, withAttributes: attributes)
        let result=UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return result!
    }

以上是关于如何在中心绘制文字到uiimage?的主要内容,如果未能解决你的问题,请参考以下文章

绘制文本并添加到 UIImage iOS 5/6

使用 Core Graphics 在 UIImage 上绘制矩形

如何将 UIImage 视图和其中的文本保存到磁盘

如何在 iOS 中在没有 UIImageView 的情况下在视图上绘制/绘制 UIIMage

如何在这样的代码中绘制可调整大小的 UIImage?

将 NSString 绘制到 UIImage