0

I've been trying to figure this out for a while. I have set constraints for each label with background color set. I set each label's line break to word wrap, but that still doesn't work. What I'm looking for is a label wrap like word wrap whether or not that exists. Thanks.

enter image description here

Super Jade
  • 5,609
  • 7
  • 39
  • 61
  • 3
    I suggest you to add `UICollectionView` inside your `UITableViewCell`. – Kuldeep Jul 31 '18 at 05:03
  • Agree with @Kuldeep. You will also need sizeWithAttributes of NSString to decide size of UICollectionViewCell. https://stackoverflow.com/questions/23134986/dynamic-cell-width-of-uicollectionview-depending-on-label-width – CRDave Jul 31 '18 at 05:19
  • I suggest you to use TagListView inside the 'UITableViewCell' – Anuraj Jul 31 '18 at 05:23
  • so, you want labels to automatically move to new line if they out of bounds, is that correct? if so, you can't do that manually using constraint. You have to calculate every labels width. Or you can use libs like https://github.com/ElaWorkshop/TagListView – axunic Jul 31 '18 at 08:56

1 Answers1

0

Here is a slightly different approach. You can customize the appearance of each tagged word with an attributed string. This has some limitations but depending on your requirements it could be a good fit for you. The below code is an example pointing you in the correct direction, however you still might need to write additional code for correctly wrapping the spaces or recognizing touch events.

let tags = ["Outdoors", "Working", "Learning"].map { " \($0) " }
let text = tags.joined(separator: "  ")
let ranges = tags.compactMap { text.range(of: $0) }

let attributedText = NSMutableAttributedString(string: text)

for range in ranges {
    attributedText.addAttributes([.backgroundColor: UIColor.green], range: NSRange(range, in: text))
}

textView.attributedText = attributedText
cnotethegr8
  • 7,342
  • 8
  • 68
  • 104