-1

All the sample code i have comea cross with just does not work with bold tags anymore. This also include italic html tags.

I am using the code from hacking swift as string extension.

var htmlAttributedString: NSAttributedString? {
        if let attributedString = try? NSAttributedString(data: Data(self.utf8), options: [.documentType: NSAttributedString.DocumentType.html], documentAttributes: nil) {
            return attributedString
        }
        else {
            return nil
        }
    }
    
    var htmlString: String {
        return htmlAttributedString?.string ?? ""
    }

Then try

let string = "<b>sample</b>"
Text(string.htmlString)

The code looks about right. Just that the bold tag does not get rendered. Anyone know of a workaround? I tried the adding html style system hardcoding font trick but it did not work as well.

I tried the markdown alternative , no luck either (but this is a different topic).

chitgoks
  • 311
  • 2
  • 6
  • 17

1 Answers1

1

Note that your htmlString property essentially converts the attributed string back to a plain text string. Accessing the NSAttributedString.string property gives you the plain text parts of the string back, without any attributes.

Since this string is to be displayed in a Text, you can use the Swift AttributedString API instead. Change the type of htmlAttributedString to AttributedString, and convert the NSAttributedString:

extension String {
    var htmlAttributedString: AttributedString {
        if let attributedString = try? NSAttributedString(data: Data(self.utf8), options: [.documentType: NSAttributedString.DocumentType.html], documentAttributes: nil) {
            return AttributedString(attributedString)
        }
        else {
            return ""
        }
    }
}

Then you can create the Text like this:

Text("<b>foo</b>bar".htmlAttributedString)

Side note: if you are working with markdown instead, you can directly create the Text using a string literal like this - no need for any AttributedStrings

Text("**foo** bar")

If your markdown string is not a literal, wrap it in a LocalizedStringKey:

Text(LocalizedStringKey(someMarkdown))
Sweeper
  • 213,210
  • 22
  • 193
  • 313
  • Thank you @Sweeper I was focused on the return type of NSAttributedString that I used .string property so that Text() would accept it. I tried now and it works. – chitgoks Oct 07 '22 at 10:36
  • just an additional question if you have an idea. when rendered, the font size became too small compared to rendering it in plain text. Tried but didnt work. Any idea of a workaround to make it bigger? Instead of. using font-size in style. – chitgoks Oct 07 '22 at 10:39
  • @chitgoks You can try adding a font attribute throughout the range of the whole string, before returning it in `htmlAttributedString`. But it seems like you are trying to display your own rich text instead of some random text on the web? If so, I recommend that you work with markdown instead - it works much more nicely with SwiftUI. – Sweeper Oct 07 '22 at 10:47
  • It seems that the default font is changed to helvetica with font size of 12 (normal behavior. I actually decided to use markdown first to avoid the hassle but even that didnt work out. I tried just Text("*test*") (removed double asterisk since it gets formatted here) but the same shows up, no formatting. – chitgoks Oct 07 '22 at 10:55
  • @chitgoks With markdown, you can change the font size and other things with the SwiftUI methods, like `.font(.largeTitle)`. `Text("**foo** bar")` works just fine for me. You can clearly see the distinction between the bold and the non-bold. – Sweeper Oct 07 '22 at 11:01
  • I agree. You are right. I think why it is not showing is because i am concatenating strings like let test = "**" + var1 + "** here is not bold anymore" but it did work when the full text with markdown are inside the string. Right now, I used a in my html string to make the font size bigger and override the default helvetica font that is being used. – chitgoks Oct 07 '22 at 11:06
  • @chitgoks Right, if you are doing that, wrap a `LocalizedStringKey` outside the markdown like my last code snippet. See [my answer here](https://stackoverflow.com/a/71349993/5133585) for why. – Sweeper Oct 07 '22 at 11:09
  • I got it to work. Reason was i didnt use the markdown attribute he he he. My bad. Im going witht he markdown route instead of html. What a pain. But happy that I understand now. Thanks bud. – chitgoks Oct 07 '22 at 11:10
  • Text(LocalizedStringKey(someMarkdown)) information REALLY helped. Instead of fighting with AttributeString, I’m changing my string data. Thanks for this. – user1204493 Jun 12 '23 at 21:14