Menu

#646 Investigate if NSAttributedString can be used instead of WKWebView

iphone_1.10
wont-fix
None
5
2020-07-21
2020-07-17
No

We have several use cases where we need a html-renderer for local html-encoded strings: card questions and answers during test, drill, and card edit, and our help pages.
Originally we have implemented these using UIWebView, which is deprecated now.
The recommended replacement is WKWebView.
But both these APIs are actually overkill for our use case. They need a lot of resources and have slow startup time.
A possible alternative could be to use NSAttributedString instead.
This should be investigated in more detail.

See also https://stackoverflow.com/questions/39999392/how-to-display-text-with-html-tags-in-ios-10-without-using-uiwebview-using-swif

"the approach is always the same: 1. turn the html into an attributedString 2. set that on the View/Control/TextView.. whatever"

Discussion

  • Christa Runge

    Christa Runge - 2020-07-17
    • Description has changed:

    Diff:

    --- old
    +++ new
    @@ -0,0 +1,10 @@
    +We have several use cases where we need a html-renderer for local html-encoded strings: card questions and answers during test, drill, and card edit, and our help pages.
    +Originally we have implemented these using UIWebView, which is deprecated now.
    +The recommended replacement is WKWebView.
    +But both these APIs are actually overkill for our use case. They need a lot of resources and have slow startup time.
    +A possible alternative could be to use NSAttributedString instead.
    +This should be investigated in more detail.
    +
    +See also https://stackoverflow.com/questions/39999392/how-to-display-text-with-html-tags-in-ios-10-without-using-uiwebview-using-swif
    +
    +"the approach is always the same: 1. turn the html into an attributedString 2. set that on the View/Control/TextView.. whatever"
    
     
  • Christa Runge

    Christa Runge - 2020-07-17
    • summary: Investigate if NSAttributedString can be used instead of UIWebView --> Investigate if NSAttributedString can be used instead of WKWebView
     
  • Mathias Kussinger

    • assigned_to: Mathias Kussinger
     
  • Mathias Kussinger

    External css:

    You can keep the CSS in a separate file in your bundle for macOS at least, as I've just had this working for me in to an NSTextView. First you need to include the stylesheet link in your HTML document's HEAD as you would usually (e.g. <link href="Base.css" type="text/css" rel="stylesheet">). Then when you create your NSAttributedString you need to pass a base URL (e.g. by using htmlUrl.deletingLastPathComponent() in Swift). – Quintin Willison May 17 '18 at 12:32

     
  • Mathias Kussinger

    The same should work with image references, too.

     
  • Mathias Kussinger

    the page history needs to be handled manually.
    And no external links!

     

    Last edit: Mathias Kussinger 2020-07-18
  • Mathias Kussinger

    The html features of NSAttributedString are very limited. No tables, no badding. It makes no sense to use it to replace the Web views.

     
  • Mathias Kussinger

    • status: open --> wont-fix
     

Log in to post a comment.