将文本转换为图像

mac*_*ent 7 text rendering image uiimageview ios

如何将文本转换为图像并在UIImageview中显示.任何人都可以知道从文本到图像的转换吗?

dun*_*cox 12

你可以开始玩这样的东西:

NSString *string = @"Some text";
UIGraphicsBeginImageContext(CGSizeMake(80, 50));
[string drawAtPoint:CGPointMake(10, 20)
           withFont:[UIFont systemFontOfSize:12]];
UIImage *result = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
Run Code Online (Sandbox Code Playgroud)

result包含带有文本的UIImage,您可以将其分配给UIImageView image属性.


Ima*_*tit 10

使用Swift 4和iOS 11,您可以选择以下6种方法之一来解决您的问题.


#1.使用NSStringdraw(at:withAttributes:)方法

在最简单的情况下,您希望将a转换StringUIImage具有某些属性的a,您可以使用draw(at:withAttributes:).以下Playground代码显示如何UIImageString使用中获取draw(at:withAttributes:):

import UIKit
import PlaygroundSupport

let text = "Hello, world"
let attributes = [
    NSAttributedString.Key.foregroundColor: UIColor.yellow,
    NSAttributedString.Key.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)
Run Code Online (Sandbox Code Playgroud)
import UIKit
import PlaygroundSupport

let text = "Hello, world"
let attributes = [
    NSAttributedString.Key.foregroundColor: UIColor.yellow,
    NSAttributedString.Key.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)
Run Code Online (Sandbox Code Playgroud)

注意,NSAttributedString有一个类似的方法叫draw(at:).


#2.使用NSStringdraw(in:withAttributes:)方法

作为替代方案draw(at:withAttributes:),您可以使用draw(in:withAttributes:).

import UIKit
import PlaygroundSupport

let text = "Hello, world"
let attributes = [
    NSAttributedString.Key.foregroundColor: UIColor.yellow,
    NSAttributedString.Key.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)
Run Code Online (Sandbox Code Playgroud)
import UIKit
import PlaygroundSupport

let text = "Hello, world"
let attributes = [
    NSAttributedString.Key.foregroundColor: UIColor.yellow,
    NSAttributedString.Key.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)
Run Code Online (Sandbox Code Playgroud)

注意,NSAttributedString有一个类似的方法叫draw(in:).


#3.使用NSStringdraw(with:options:attributes:context:)方法

作为一种替代draw(at:withAttributes:)draw(in:),你可以使用draw(with:options:attributes:context:).请注意,Apple提供了一些建议draw(with:options:attributes:context:):

此方法默认使用基线原点.如果usesLineFragmentOrigin未指定,则将忽略矩形的高度,并将操作视为单行渲染.

import UIKit
import PlaygroundSupport

let text = "Hello, world"
let attributes = [
    NSAttributedString.Key.foregroundColor: UIColor.yellow,
    NSAttributedString.Key.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)
Run Code Online (Sandbox Code Playgroud)
import UIKit
import PlaygroundSupport

let text = "Hello, world"
let attributes = [
    NSAttributedString.Key.foregroundColor: UIColor.yellow,
    NSAttributedString.Key.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)
Run Code Online (Sandbox Code Playgroud)

注意,NSAttributedString有一个类似的方法叫draw(with:options:context:).


#4.使用CALayerrender(in:)方法

如果你想捕捉的文本UILabel,UITextFieldUITextView到一个UIImage,就可以使用render(in:).以下Playground代码显示如何对UILabel使用的内容文本进行快照render(in:):

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)
Run Code Online (Sandbox Code Playgroud)
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)
Run Code Online (Sandbox Code Playgroud)

#5.使用UIViewdrawHierarchy(in:afterScreenUpdates:)方法

如果你想捕捉的文本UILabel,UITextFieldUITextView到一个UIImage,就可以使用drawHierarchy(in:afterScreenUpdates:).请注意,Apple提供了一些建议drawHierarchy(in:afterScreenUpdates:):

如果要将图形效果(如模糊)应用于视图快照,请使用此方法.这种方法没有方法那么快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)
Run Code Online (Sandbox Code Playgroud)
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)
Run Code Online (Sandbox Code Playgroud)

#6.使用UIViewsnapshotView(afterScreenUpdates:)方法

如果可以从快照操作中获取UIView而不是a UIImage,则可以使用snapshotView(afterScreenUpdates:).以下Playground代码显示如何将a的内容文本快照UILabel到一个UIViewusing 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()

let view = label.snapshotView(afterScreenUpdates: true)
PlaygroundPage.current.liveView = view
Run Code Online (Sandbox Code Playgroud)