I have had the opportunity to learn so many things while working on Proton. Just as much as I like sharing my code, I also enjoy talking about the challenges and the wins. This article focuses on yet another feature in Proton which, I believe, should have been available out of the box in iOS. If you are new here and before diving into the details, would like to know more about Proton, Building a native editor for iOS is a good place to start.
Often there are times when we want to add background to the text in a text view. The most simplest, and probably best, approach is to use backgroundColor attribute. You can provide any UIColor
to a given range, and iOS takes care of rendering that color in the background of the text.
Text with .backgroundColor attribute
The only code you need is to add attribute backgroundColor: UIColor.cyan
to the list of NSAttributedString.Key
in the attributedText
property of UITextView
. While this works for most cases, it may feel too vanilla. You might want a fancier formatting like adding shadows or borders. At the very least, it is very likely that you would want rounded corners to the background. Unfortunately, none of these properties may be modified by just using attributes.
#uitextview #ios #swift #background