How to Convert Text to Image and show in UIImageview. Can anyone know the conversion from Text to Image?
相关问题
- Views base64 encoded blob in HTML with PHP
- How to get the background from multiple images by
- Core Data lightweight migration crashes after App
- React Native Inline style for multiple Text in sin
- How can I implement password recovery in an iPhone
相关文章
- 现在使用swift开发ios应用好还是swift?
- 放在input的text下文本一直出现一个/(即使还没输入任何值)是什么情况
- UITableView dragging distance with UIRefreshContro
- TCC __TCCAccessRequest_block_invoke
- Use savefig in Python with string and iterative in
- Where does a host app handle NSExtensionContext#co
- Where does this quality loss on Images come from?
- Swift - hide pickerView after value selected
[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.
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.With Swift 4 and iOS 11, 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:)
: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.