Search code examples
iosswiftbackgroundbackground-colornsattributedstring

Swift: How can I create text with coloured block rectangular background


I have looked into using NSAttributedString but that will colour background based on line length but I would like to create coloured rectangular backgrounds that span width of view/label.

This is how I'd like my text to show with rectangular block coloured backgrounds for each paragraph with white space in between

enter image description here


Solution

  • If you want it to look like your image, you can stack labels in a UIStackView and change all the background color of the labels to your custom color