0

Currently, I have. Current UI Label

However, I would like it to have space between the lines so the background shows and a background color that has a dynamic width for each line, something similar to

Desired Label Look

I have tried converting my label into a text view, and using a custom class. This does not get the desired outcome. When a new cell is created, it uses the following code.

cell.label.backgroundColor = UIColor.white
        return cell

So, how could I get the text to get spaces between the lines as in the picture with the background showing through, and the width for each line based off the text as in the picture?

Raim Khalil
  • 387
  • 3
  • 19
  • I have already tried https://stackoverflow.com/questions/27360006/uitextview-uilabel-with-background-but-with-spacing-between-lines but to no avail. – Raim Khalil Jan 21 '18 at 04:31

1 Answers1

1

Are you sure you've uploaded the images you intended to? Your Desired Label Look does not have a background colour that follows the text with like you describe.

Here's an example to get you going:

let string = "Hello\n\nIs it me you're\n\nlooking for"
var attributedString = NSMutableAttributedString(string:string)

string.enumerateLines { (line, stuff) in
    guard let range = string.range(of: line) else { return }
    let nsRange = NSRange(range, in: line)
    attributedString.addAttribute(.backgroundColor,
                                  value: UIColor.red,
                                  range: nsRange)
}

Here I am making each line of the string red individually to get the effect you describe.

I used double line breaks to achieve the separation, as I don't think it's possible to simply get the separated line effect with a single NSAttributedString.

This results in:

enter image description here

.

Terje
  • 980
  • 9
  • 15
  • Which part is missing in my answer? – Terje Jan 21 '18 at 17:52
  • I was wondering which part you were missing since you said my answer covered half your question. – Terje Jan 21 '18 at 18:05
  • the problem is is that my posts are made out of items from the database ... I can't just split apart the string by hand using line breaks. The string may be anything from the databas. How would I implement something like that with words from a database, dynamically? – Raim Khalil Jan 21 '18 at 20:26
  • the picture you posted is what I'm looking for , however I am wondering how to achieve this effect with dynamic data from a database . – Raim Khalil Jan 21 '18 at 20:36
  • Raim - you should mark this as correct. Just because you need additional functionality not stated in the question does not mean that this answer is not correct. The functionality you seek is not easy to explain as it would take several steps to obtain. This is the answer to your original question. I stated earlier that it was not the entire answer and I deleted that comment because this answer actually answers the question you asked. Find out in another post how to break up the lines. – agibson007 Jan 21 '18 at 22:40
  • Thank you @agibson007. Raim, you might want to look at something like this: https://stackoverflow.com/questions/30450434/figure-out-size-of-uilabel-based-on-string-in-swift/30450559 - if you split your original string into words, then make a new string and start adding words to it, one by one. Measure your string width as you go along, stopping right before you've reached your full line width. Insert a line break and repeat for the remaining lines. – Terje Jan 22 '18 at 06:14