0

I have a UILabel that contains an NSMutableAttributedString attached to it which was generated dynamically from a model.

What I'm looking to accomplish is if ever the UILabel's dynamic height reaches a specific height with the NSMutableAttributedString in it, I would like to add another UILabel to the subview or maybe multiple more UILabels if the NSMutableAttributedString is too long.

An array of NSAttributedString from NSMutableAttributedString would be ideal.

Let's say I have this very long NSMutableAttributedString, then I've set my height limit to 100 for one UILabel.

Ex:

Given String:

* qwe
* qwe
* qwe
* qwe
* qwe
* qwe
* qwe
* qwe
* qwe
* qwe
* qwe
* qwe
* qwe
* qwe
* qwe
* qwe
* qwe
* qwe
* qwe
* qwe
* qwe
* qwe
* qwe
* qwe
* qwe
* qwe
* qwe
* qwe
* qwe
* qwe
* qwe
* qwe

Height limit: Let's say we calculated that 4 lines would fit per 100 height. This would result into. An array of NSMutableAttributedStrings

let result = ["* qwe\n * qwe\n * qwe\n * qwe\n",
              "* qwe\n * qwe\n * qwe\n * qwe\n",
              "* qwe\n * qwe\n * qwe\n * qwe\n",
              "* qwe\n * qwe\n * qwe\n * qwe\n"]

This would be easy to populate into multiple UILabels.

Jojo Narte
  • 2,767
  • 2
  • 30
  • 52
  • Cant you just force your label to have 100 height and auto resize the text inside to fit? – Tj3n Mar 21 '18 at 11:46
  • @Tj3n that wouldn't look good on my use-case. I'm actually integrating this solution for a PDF generated from a UITableView. – Jojo Narte Mar 21 '18 at 12:22
  • @JojoNarte Did you get a solution for the same ? I am also facing a similar issue where I need to split attributed string between multiple UIlabels of a certain max height only. – sac Jun 03 '21 at 11:03

0 Answers0