Home > Net >  How to add image between text in swift?
How to add image between text in swift?

Time:01-18

I have a string with a lot of text and I want to add a picture between this text. The SwiftUI methods don't work for me. Is it possible to do this in UIKit?

This is what I want the string to look like:

enter image description here

I tried this, but it gives an error

let article = "Start "   String(UIImage(named: "image"))   "End"

CodePudding user response:

You can do this with SwiftUI

Text("UIImage \(Image(systemName: "photo"))")

Then convert to a UIView with

UIHostingController(rootView: Text("UIImage \(Image(systemName: "photo"))")).view

You might need some constraints to keep it visible.

CodePudding user response:

I won't give a proper solution here but an idea of how you can achieve what you want. What you're trying to concatenate is a String and a UImageView which cannot work. If you want to mix Strings and Images you need to have separated views. In SwiftUI you'd have something like:

VStack {
  Text("Text above image) 
  Image(named: "The name of your image")
  Text("Text below image") 
}

You can also do that with UIKit of course, there you'd need some proper setup with 2 UILabels and a UIImageView and then do the layout constraints on all of those.

  • Related