Reputation: 387
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
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?
Upvotes: 0
Views: 1072
Reputation: 1000
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:
.
Upvotes: 1