如何将文本添加到iOS Swift中的图像

我已经环顾四周,并没有成功地弄清楚如何把文本,覆盖在图像上,然后将它们组合成一个单一的UIImage。

我已经穷尽谷歌使用search条件,我能想到的,所以如果任何人有一个解决scheme或至less有一个提示,他们可以指出,将不胜感激。

好的…我明白了:

func textToImage(drawText: NSString, inImage: UIImage, atPoint: CGPoint) -> UIImage{ // Setup the font specific variables var textColor = UIColor.whiteColor() var textFont = UIFont(name: "Helvetica Bold", size: 12)! // Setup the image context using the passed image let scale = UIScreen.mainScreen().scale UIGraphicsBeginImageContextWithOptions(inImage.size, false, scale) // Setup the font attributes that will be later used to dictate how the text should be drawn let textFontAttributes = [ NSFontAttributeName: textFont, NSForegroundColorAttributeName: textColor, ] // Put the image into a rectangle as large as the original image inImage.drawInRect(CGRectMake(0, 0, inImage.size.width, inImage.size.height)) // Create a point within the space that is as bit as the image var rect = CGRectMake(atPoint.x, atPoint.y, inImage.size.width, inImage.size.height) // Draw the text into an image drawText.drawInRect(rect, withAttributes: textFontAttributes) // Create a new image out of the images we have created var newImage = UIGraphicsGetImageFromCurrentImageContext() // End the context now that we have the image we need UIGraphicsEndImageContext() //Pass the image back up to the caller return newImage } 

要调用它,你只需传入一个图像:

 textToImage("000", inImage: UIImage(named:"thisImage.png")!, atPoint: CGPointMake(20, 20)) 

以下链接帮助我得到这个直线:

Swift – 用drawInRect绘制文本:withAttributes:

如何在Objective-C(iOS)的图像上写文本?

最初的目标是创build一个dynamic的图像,我可以在AnnotaionView使用,比如在地图上给定的位置放置一个价格,这对于它来说非常AnnotaionView 。 希望这可以帮助有人试图做同样的事情。

对于迅捷3:

  func textToImage(drawText text: NSString, inImage image: UIImage, atPoint point: CGPoint) -> UIImage { let textColor = UIColor.white let textFont = UIFont(name: "Helvetica Bold", size: 12)! 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) text.draw(in: rect, withAttributes: textFontAttributes) let newImage = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() return newImage! } 

对于迅捷4:

  func textToImage(drawText text: String, inImage image: UIImage, atPoint point: CGPoint) -> UIImage { let textColor = UIColor.white let textFont = UIFont(name: "Helvetica Bold", size: 12)! let scale = UIScreen.main.scale UIGraphicsBeginImageContextWithOptions(image.size, false, scale) let textFontAttributes = [ NSAttributedStringKey.font: textFont, NSAttributedStringKey.foregroundColor: textColor, ] as [NSAttributedStringKey : Any] image.draw(in: CGRect(origin: CGPoint.zero, size: image.size)) let rect = CGRect(origin: point, size: image.size) text.draw(in: rect, withAttributes: textFontAttributes) let newImage = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() return newImage! } 

我简单的解决scheme

 func generateImageWithText(text: String) -> UIImage { let image = UIImage(named: "imageWithoutText")! let imageView = UIImageView(image: image) imageView.backgroundColor = UIColor.clearColor() imageView.frame = CGRectMake(0, 0, image.size.width, image.size.height) let label = UILabel(frame: CGRectMake(0, 0, image.size.width, image.size.height)) label.backgroundColor = UIColor.clearColor() label.textAlignment = .Center label.textColor = UIColor.whiteColor() label.text = text UIGraphicsBeginImageContextWithOptions(label.bounds.size, false, 0); imageView.layer.renderInContext(UIGraphicsGetCurrentContext()!) label.layer.renderInContext(UIGraphicsGetCurrentContext()!) let imageWithText = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext(); return imageWithText } 

你也可以做一个CATextLayer。

  // 1 let textLayer = CATextLayer() textLayer.frame = someView.bounds // 2 var string = "" for _ in 1...20 { string += "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce auctor arcu quis velit congue dictum. " } textLayer.string = string // 3 let fontName: CFStringRef = "Noteworthy-Light" textLayer.font = CTFontCreateWithName(fontName, fontSize, nil) // 4 textLayer.foregroundColor = UIColor.darkGrayColor().CGColor textLayer.wrapped = true textLayer.alignmentMode = kCAAlignmentLeft textLayer.contentsScale = UIScreen.mainScreen().scale someView.layer.addSublayer(textLayer) 

http://www.raywenderlich.com/90488/calayer-in-ios-with-swift-10-examples

对于迅捷4:

 func textToImage(drawText text: NSString, inImage image: UIImage, atPoint point: CGPoint) -> UIImage { let scale = UIScreen.main.scale UIGraphicsBeginImageContextWithOptions(image.size, false, scale) image.draw(in: CGRect(origin: CGPoint.zero, size: image.size)) let rect = CGRect(origin: point, size: image.size) let paragraphStyle = NSMutableParagraphStyle() paragraphStyle.alignment = .center let attrs = [NSAttributedStringKey.font: UIFont(name: "Helvetica Bold", size: 12)!,NSAttributedStringKey.foregroundColor : UIColor.white , NSAttributedStringKey.paragraphStyle: paragraphStyle] text.draw(with: rect, options: .usesLineFragmentOrigin, attributes: attrs, context: nil) let newImage = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() return newImage! }