RichTextView

Ahmed Elkady
Top Hat Engineering Blog
2 min readDec 1, 2018

--

Imagine you are an iOS developer, and you’re asked to build a label that renders HTML. That’s easy, you can just use NSAttributedString and pass in an HTML document type, letting that work its magic.

What if you’re now asked to display Markdown along with HTML? Well, you can either find a Markdown parser or build your own, and somehow reconcile that with the HTML.

What if you are then asked to display LaTeX, or YouTube/Vimeo links? Then you have to worry about WKWebView and proper layouts and constraints.

Well, no more.

Introducing RichTextView, a subclass of UIView that handles all of the above rich text data types

How It Works

In order for the RichTextView to handle the various use cases it might encounter, the input string needs to be formatted correctly. You can check out the appropriate input formats at our project website.

Once the input is formatted appropriately, the Youtube/Vimeo links are rendered using WKWebViews and the rest of the rich text using NSAttributedStrings in UILabels . Then, the RichTextView will use SnapKit to dynamically constrain the views one on top of another in the order they appear in the input string.

What If I Want RichTextView To Support Additional Rich Text Types?

RichTextView is designed to easily support adding more rich text types as necessary. If you would like to see something supported feel free to contribute to our repo.

Summary

In short, RichTextView is a powerful tool that can be used to render various kinds of rich text types. It is designed and built in Swift, and once implemented in your project will handle any kind of input you throw at it, as long as it is formatted appropriately.

RichTextView can be integrated into your iOS app using Cocoapods or Carthage, making it easy to start using in your app today!

--

--