在 iOS 上将文本转换为图像
如何将文本转换为图像并在 UIImageview 中显示。 谁能知道从文本到图像的转换吗?
How to convert Text to Image and show in UIImageview.
Can anyone know the conversion from Text to Image?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用 Swift 5 和 iOS 12,您可以选择以下 6 种方法之一来解决您的问题。
#1.使用
NSString
的draw(at:withAttributes:)
方法在最简单的情况下,您要将
String
转换为UIImage
对于某些属性,您可以使用draw(at:withAttributes :)
。以下 Playground 代码展示了如何使用draw(at:withAttributes:)
从String
获取UIImage
:请注意
NSAttributedString< /code> 有一个类似的方法,称为
draw(at:)
。#2.使用
NSString
的draw(in:withAttributes:)
方法作为
draw(at:withAttributes:)
的替代方法,您可以使用draw(in:withAttributes:)
。请注意,
NSAttributedString
有一个类似的方法,称为draw(in:)
。#3。使用
NSString
的draw(with:options:attributes:context:)
方法作为
draw(at:withAttributes:)
和 < code>draw(in:),您可以使用draw (带有:选项:属性:上下文:)
。请注意,Apple 对draw(with:options:attributes:context:)
有一些建议:请注意,
NSAttributedString
有一个类似的方法,称为draw(with:options:context:)
。#4。使用
CALayer
的render(in:)
方法如果你想捕获
UILabel
、UITextField
的文本或UITextView
到UIImage
,您可以使用渲染(in:)
。以下 Playground 代码展示了如何使用render(in:)
对UILabel
的内容文本进行快照:#5。使用
UIView
的drawHierarchy(in:afterScreenUpdates:)
方法如果您想捕获
UILabel
、UITextField
的文本code> 或UITextView
到UIImage
,您可以使用drawHierarchy(in:afterScreenUpdates:)
。请注意,Apple 对drawHierarchy(in:afterScreenUpdates:)
有一些建议:#6。使用
UIView
的snapshotView(afterScreenUpdates:)
方法如果您可以获取
UIView
而不是UIImage
> 在快照操作中,您可以使用snapshotView(afterScreenUpdates:)
。以下 Playground 代码展示了如何使用
snapshotView(afterScreenUpdates:)
将UILabel
的内容文本快照到UIView
中:With Swift 5 and iOS 12, you can choose one the 6 following ways in order to solve your problem.
#1. Using
NSString
'sdraw(at:withAttributes:)
methodIn the simplest case where you want to convert a
String
to aUIImage
with some attributes, you can usedraw(at:withAttributes:)
. The following Playground codes show how to get anUIImage
from aString
usingdraw(at:withAttributes:)
:Note that
NSAttributedString
has a similar method calleddraw(at:)
.#2. Using
NSString
'sdraw(in:withAttributes:)
methodAs an alternative to
draw(at:withAttributes:)
, you can usedraw(in:withAttributes:)
.Note that
NSAttributedString
has a similar method calleddraw(in:)
.#3. Using
NSString
'sdraw(with:options:attributes:context:)
methodAs an alternative to
draw(at:withAttributes:)
anddraw(in:)
, you can usedraw(with:options:attributes:context:)
. Note that Apple has some recommendations fordraw(with:options:attributes:context:)
:Note that
NSAttributedString
has a similar method calleddraw(with:options:context:)
.#4. Using
CALayer
'srender(in:)
methodIf you want to capture the text of a
UILabel
,UITextField
orUITextView
to aUIImage
, you can userender(in:)
. The following Playground codes show how to snapshot the content text of aUILabel
usingrender(in:)
:#5. Using
UIView
'sdrawHierarchy(in:afterScreenUpdates:)
methodIf you want to capture the text of a
UILabel
,UITextField
orUITextView
to aUIImage
, you can usedrawHierarchy(in:afterScreenUpdates:)
. Note that Apple has some recommendations fordrawHierarchy(in:afterScreenUpdates:)
:#6. Using
UIView
'ssnapshotView(afterScreenUpdates:)
methodIf it's OK for you to get a
UIView
instead of aUIImage
from your snapshot operation, you can usesnapshotView(afterScreenUpdates:)
. The following Playground code shows how to snapshot the content text of aUILabel
into aUIView
usingsnapshotView(afterScreenUpdates:)
:您可以开始尝试这样的事情:
result
包含带有文本的 UIImage,您可以将其分配给 UIImageViewimage
属性。You can start playing around with something like this:
result
contains the UIImage with the text in it, and you can assign it to the UIImageViewimage
property.SWIFT 3 :
为 UIImage 创建一个扩展,以便您可以在任何地方使用它:
现在无论您在哪里需要使用它从文本创建图像:
// 这种标签的自定义是根据您的需要,您需要哪种字体,什么背景颜色。随意改变。
SWIFT 3 :
Create an extension for UIImage so that you can use it everywhere:
Now where ever you need to use this to create an image from Text :
// This customisation of label is according to your needs which font , what background color you need. Feel free to change.
[yourImageView addSubview:textView];
[canvas addSubview:passingImageView];
你应该采用一个 UIView 并在内部采用 UIImageview ,上面的代码应该可以解决问题。这里的画布是 UIView。
[yourImageView addSubview:textView];
[canvas addSubview:passingImageView];
you should take a UIView and inside take UIImageview and the above code should do the trick.Here canvas is UIView.