gonzalezreal / swift-markdown-ui

Display and customize Markdown text in SwiftUI
MIT License
2.53k stars 318 forks source link

Images are not resizing to desired width&height. #237

Closed george-bh closed 1 year ago

george-bh commented 1 year ago

Description I'm trying to set the width&height of the image. The image is exceeding these values.

Checklist

Steps to reproduce

  1. Include an image into the markdown. You can use the example below
let text = "TOMMY HILFIGER Store\n![](https://aez.acc.ece.esolut.org/fileadmin/_processed_/a/2/csm_AEZ-TH-MEMBERVIPNIGHTS-08092022_a99101572d.jpg)\n\n"
Markdown { text }
                .markdownTheme(
                    .init().image { configuration in
                        configuration.label
                            .frame(width: 30, height: 30)
                    }
                )
  1. Image exceeds the screen size

Expected behavior Image should have the desired width&height.

Screenshots Screenshot

Version information

gonzalezreal commented 1 year ago

Hi @odariusgeorge,

The theme image property is a block style that allows customizing the appearance of isolated images instead of images laid out within a line of text. I will improve the documentation to make that clear.

We cannot use the image block style because SwiftUI doesn't allow concatenating Text with any View implementation (which is what a block style returns), just with other Text views. But SwiftUI allows creating a Text view from an Image view, which MarkdownUI uses for inline images.

In this particular case, the workaround is to put the image in a different paragraph:

Markdown {
  """
  TOMMY HILFIGER Store

  ![](https://aez.acc.ece.esolut.org/fileadmin/_processed_/a/2/csm_AEZ-TH-MEMBERVIPNIGHTS-08092022_a99101572d.jpg)
  """
}
.markdownBlockStyle(\.image) { configuration in
  configuration.label
    .frame(width: 30, height: 30)
}

If that workaround is not suitable for your use case, MarkdownUI offers the InlineImageProvider protocol as a customization point for inline images. You can create an implementation that resizes the images as you wish.

I am closing this issue for the reasons mentioned above. However, there is an ongoing discussion about implementing an inline image provider that resizes inline images to the current font size. It is related to what you want to achieve, so feel free to jump in and share your thoughts.