-1

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? I want the text and pictures to be on the same line (string or something else). The main thing is that it can be transferred to the label!

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"
SwiftCoder
  • 61
  • 1
  • 8
  • Look for (NS)AttributedString, simple "text" is just letter, it can't have image. – Larme Jan 17 '23 at 16:27
  • @Larme I tried this method, but as I understand it, you can only add: text-picture-text! I need it to be all text in one string. – SwiftCoder Jan 17 '23 at 16:39
  • you try to convert a UIImage to a String, this cannot work. – ZAY Jan 17 '23 at 17:05
  • see here; https://stackoverflow.com/questions/38479233/how-can-i-add-image-in-uitextview – john elemans Jan 17 '23 at 17:31
  • Explain what's supposed to be `String(UIImage(named: "image"))`. – Larme Jan 17 '23 at 17:59
  • @Larme I want to have text and pictures in one line and then pass that line to the label. I understand that my code is not correct! – SwiftCoder Jan 17 '23 at 20:38
  • 1
    "but as I understand it, you can only add: text-picture-text! I need it to be all text in one string." that part is unclear, as `NSAttributedString` seems to be the solution. See https://www.hackingwithswift.com/example-code/system/how-to-insert-images-into-an-attributed-string-with-nstextattachment – Larme Jan 17 '23 at 21:11
  • @Larme Everything works! Thank you! I found my mistake. – SwiftCoder Jan 17 '23 at 21:51

2 Answers2

1

Here is the answer to my question

   // function to add an image
    func settingsForImage(image: String) -> NSAttributedString {
        let imageAttachment = NSTextAttachment()
        imageAttachment.image = UIImage(named: image)
        // image size
        imageAttachment.bounds = CGRect(x: 0, y: 0, width: 400, height: 200)
        let imageString = NSAttributedString(attachment: imageAttachment)
        return imageString
    }
    
    let textStart = "Start \n \n"
    let image = settingsForImage(image: "your_image")
    let textEnd = "\n \n End"
    let image1 = settingsForImage(image: "your_image")
    
    let attributedString = NSMutableAttributedString(string: textStart)
    attributedString.append(image)
    attributedString.append(NSMutableAttributedString(string: textEnd))
    attributedString.append(image1)

    textLabel.attributedText = attributedString
SwiftCoder
  • 61
  • 1
  • 8
-1

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.

baronfac
  • 348
  • 3
  • 9