将文本转换为图像
如何将文本转换为图像并在UIImageView中显示。 任何人都可以知道从文本到图像的转换?
你可以开始玩这样的东西:
NSString *string = @"Some text"; UIGraphicsBeginImageContext(CGSizeMake(80, 50)); [string drawAtPoint:CGPointMake(10, 20) withFont:[UIFont systemFontOfSize:12]]; UIImage *result = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext();
result
包含带有文本的UIImage,并且可以将其分配给UIImageView image
属性。
使用Swift 4和iOS 11,您可以select以下六种方法之一来解决您的问题。
#1。 使用NSString
的draw(at:withAttributes:)
方法
在最简单的情况下,你想用一些属性将String
转换为UIImage
,你可以使用draw(at:withAttributes:)
。 下面的游乐场代码显示了如何使用draw(at:withAttributes:)
从String
获取UIImage
:
import UIKit import PlaygroundSupport let text = "Hello, world" let attributes = [ NSAttributedStringKey.foregroundColor: UIColor.yellow, NSAttributedStringKey.font: UIFont.systemFont(ofSize: 22) ] let textSize = text.size(withAttributes: attributes) UIGraphicsBeginImageContextWithOptions(textSize, true, 0) text.draw(at: CGPoint.zero, withAttributes: attributes) let image = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() PlaygroundPage.current.liveView = UIImageView(image: image)
import UIKit import PlaygroundSupport let text = "Hello, world" let attributes = [ NSAttributedStringKey.foregroundColor: UIColor.yellow, NSAttributedStringKey.font: UIFont.systemFont(ofSize: 22) ] let textSize = text.size(withAttributes: attributes) let renderer = UIGraphicsImageRenderer(size: textSize) let image = renderer.image(actions: { context in text.draw(at: CGPoint.zero, withAttributes: attributes) }) PlaygroundPage.current.liveView = UIImageView(image: image)
请注意, NSAttributedString
有一个类似的方法叫做draw(at:)
。
#2。 使用NSString
的draw(in:withAttributes:)
方法
作为draw(at:withAttributes:)
的替代方法draw(at:withAttributes:)
,可以使用draw(in:withAttributes:)
。
import UIKit import PlaygroundSupport let text = "Hello, world" let attributes = [ NSAttributedStringKey.foregroundColor: UIColor.yellow, NSAttributedStringKey.font: UIFont.systemFont(ofSize: 22) ] let textSize = text.size(withAttributes: attributes) UIGraphicsBeginImageContextWithOptions(textSize, true, 0) let rect = CGRect(origin: .zero, size: textSize) text.draw(in: rect, withAttributes: attributes) let image = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() PlaygroundPage.current.liveView = UIImageView(image: image)
import UIKit import PlaygroundSupport let text = "Hello, world" let attributes = [ NSAttributedStringKey.foregroundColor: UIColor.yellow, NSAttributedStringKey.font: UIFont.systemFont(ofSize: 22) ] let textSize = text.size(withAttributes: attributes) let renderer = UIGraphicsImageRenderer(size: textSize) let image = renderer.image(actions: { context in let rect = CGRect(origin: .zero, size: textSize) text.draw(in: rect, withAttributes: attributes) }) PlaygroundPage.current.liveView = UIImageView(image: image)
请注意, NSAttributedString
有一个类似的方法叫做draw(in:)
。
#3。 使用NSString
的draw(with:options:attributes:context:)
方法
作为draw(at:withAttributes:)
的替代方法draw(at:withAttributes:)
和draw(in:)
,可以使用draw(with:options:attributes:context:)
。 请注意,苹果有一些关于draw(with:options:attributes:context:)
build议draw(with:options:attributes:context:)
:
此方法默认使用基准原点。 如果没有指定
usesLineFragmentOrigin
,矩形的高度将被忽略,操作被认为是单行渲染。
import UIKit import PlaygroundSupport let text = "Hello, world" let attributes = [ NSAttributedStringKey.foregroundColor: UIColor.yellow, NSAttributedStringKey.font: UIFont.systemFont(ofSize: 22) ] let textSize = text.size(withAttributes: attributes) UIGraphicsBeginImageContextWithOptions(textSize, true, 0) let rect = CGRect(origin: .zero, size: textSize) text.draw(with: rect, options: [.usesLineFragmentOrigin], attributes: attributes, context: nil) let image = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() PlaygroundPage.current.liveView = UIImageView(image: image)
import UIKit import PlaygroundSupport let text = "Hello, world" let attributes = [ NSAttributedStringKey.foregroundColor: UIColor.yellow, NSAttributedStringKey.font: UIFont.systemFont(ofSize: 22) ] let textSize = text.size(withAttributes: attributes) let renderer = UIGraphicsImageRenderer(size: textSize) let image = renderer.image(actions: { context in text.draw(with: .zero, options: [.usesLineFragmentOrigin], attributes: attributes, context: nil) }) PlaygroundPage.current.liveView = UIImageView(image: image)
请注意, NSAttributedString
有一个类似的方法叫做draw(with:options:context:)
。
#4。 使用CALayer
的render(in:)
方法
如果要将UILabel
, UITextField
或UITextView
的文本捕获到UIImage
,则可以使用render(in:)
。 以下游乐场代码显示如何使用render(in:)
快照UILabel
的内容文本:
import UIKit import PlaygroundSupport let label = UILabel(frame: .zero) label.textColor = .yellow label.font = UIFont.systemFont(ofSize: 22) label.text = "Hello, world" label.sizeToFit() UIGraphicsBeginImageContextWithOptions(label.frame.size, true, 0) guard let context = UIGraphicsGetCurrentContext() else { exit(0) } label.layer.render(in: context) let image = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() PlaygroundPage.current.liveView = UIImageView(image: image)
import UIKit import PlaygroundSupport let label = UILabel(frame: .zero) label.textColor = .yellow label.font = UIFont.systemFont(ofSize: 22) label.text = "Hello, world" label.sizeToFit() let renderer = UIGraphicsImageRenderer(size: label.frame.size) let image = renderer.image(actions: { context in label.layer.render(in: context.cgContext) }) PlaygroundPage.current.liveView = UIImageView(image: image)
#5。 使用UIView
的drawHierarchy(in:afterScreenUpdates:)
方法
如果要将UILabel
, UITextField
或UITextView
的文本捕获到UIImage
,可以使用drawHierarchy(in:afterScreenUpdates:)
。 请注意,苹果有一些build议drawHierarchy(in:afterScreenUpdates:)
:
如果要将graphics效果(如模糊效果)应用于视图快照,请使用此方法。 这个方法不如
snapshotView(afterScreenUpdates:)
方法快。
import UIKit import PlaygroundSupport let label = UILabel(frame: .zero) label.textColor = .yellow label.font = UIFont.systemFont(ofSize: 22) label.text = "Hello, world" label.sizeToFit() UIGraphicsBeginImageContextWithOptions(label.frame.size, true, 0) _ = label.drawHierarchy(in: label.bounds, afterScreenUpdates: true) let image = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() PlaygroundPage.current.liveView = UIImageView(image: image)
import UIKit import PlaygroundSupport let label = UILabel(frame: .zero) label.textColor = .yellow label.font = UIFont.systemFont(ofSize: 22) label.text = "Hello, world" label.sizeToFit() let renderer = UIGraphicsImageRenderer(size: label.frame.size) let image = renderer.image(actions: { context in _ = label.drawHierarchy(in: label.bounds, afterScreenUpdates: true) }) PlaygroundPage.current.liveView = UIImageView(image: image)
#6。 使用UIView
的snapshotView(afterScreenUpdates:)
方法
如果可以从快照操作中获取UIView
而不是UIImage
,则可以使用snapshotView(afterScreenUpdates:)
。 以下Playground代码显示如何使用snapshotView(afterScreenUpdates:)
将UILabel
的内容文本快照到UIView
:
import UIKit import PlaygroundSupport let label = UILabel(frame: .zero) label.textColor = .yellow label.font = UIFont.systemFont(ofSize: 22) label.text = "Hello, world" label.sizeToFit() let view = label.snapshotView(afterScreenUpdates: true) PlaygroundPage.current.liveView = view
[yourImageView addSubview:textView]; [canvas addSubview:passingImageView];
UIGraphicsBeginImageContext(canvas.bounds.size); [canvas.layer renderInContext:UIGraphicsGetCurrentContext()]; UIImage *resultingImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return resultingImage;
你应该把UIView和里面拿UIImageview和上面的代码应该做的trick.Herecanvas是UIView。
SWIFT 3:
为UIImage创build一个扩展,以便您可以在任何地方使用它:
extension UIImage { class func imageWithLabel(label: UILabel) -> UIImage { UIGraphicsBeginImageContextWithOptions(label.bounds.size, false, 0.0) label.layer.render(in: UIGraphicsGetCurrentContext()!) let img = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() return img! } }
现在,您需要使用此function从文本创build图像:
//这个标签的自定义是根据你的需要select哪种字体,你需要什么背景颜色。 随意更改。
let label = UILabel(frame: CGRect(x: 0, y: 0, width: 300, height: 20)) label.numberOfLines = 0 label.textAlignment = .center label.textColor = UIColor.white label.backgroundColor = UIColor.black label.font = UIFont(name: "Montserrat", size: 17) label.text = "YOUR TEXT HERE" label.sizeToFit() let image = UIImage.imageWithLabel(label: label)