iOS Question Markdown to attributed strings

Discussion in 'iOS Questions' started by nwhitfield, Aug 22, 2019.

  1. nwhitfield

    nwhitfield Active Member Licensed User

    Does anyone have a good solution for this? Ideally one that will work for both B4i and B4A. There are plenty of libraries around in both Swift and Objective C, but not so much for Java.

    I've tried playing around a bit with the Markdown-to-HTML code here and have a very rudimentary version, but I know it's going to choke on multi-line elements or it people start doing things like bold inside a link description.
  2. Erel

    Erel Administrator Staff Member Licensed User

  3. nwhitfield

    nwhitfield Active Member Licensed User

    Well, I've done a bit of lateral thinking, and I'll post the code here when I've tidied it up; I reckon with a bit of time spend getting up to speed, I could wrap some of the full markdown/commonmark libraries, but I have deadlines so...

    The application is not using huge amounts of markdown - it's things like headings, bold, italics, in news items, and reminders to people about events. And, though tapping links in text on the phone is handy, especially on small devices it can be imprecise. So, I'm doing two things server side:

    1. Scan the text, and extra all the links that have been properly marked up (to encourage the editors to do so) with titles; those are now included as part of the JSON payload that the app receives, in a 'Buttons' array. The intention is that when you tap on a calendar entry or news story, an action sheet with all the marked up links will appear.
    2. Render the markdown to html on the server, because I don't want to reinvent the wheel. Then strip all tags from it except b, i, em, strong, h1-h6 and li, and include the result in the JSON payload. This 'simple html' is plenty for the items I'm dealing with.

    What next?
    3a. You can load that simple HTML directly into a label as attributed text, using the example code from Brandsum here:
    This works, except that where the rest of the app has fairly big text, in a sans-serif font, it loads up in a smaller serif one.

    3b. Or, my html2cs code that is almost done is under 100 lines of b4x and will convert it for you, allowing you to select the base font size and colour more easily.

    A little more testing to do, then I'll add the code to this thread.
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice