1

I am trying to display the following

enter image description here

But when setting the text background color it extends the entire background and fills much than I would like as I am just trying to apply a "highlight" text feature. Any ideas would be greatly appreciated

nathan
  • 9,329
  • 4
  • 37
  • 51
Will Jamieson
  • 918
  • 1
  • 16
  • 32
  • Subviews? Subclassing? Without a bit of code it's hard to say. Right now I see a *container* view - background color is either white or clear - with 2, maybe 3 subviews, each a `UILabel` with a red background and white font color. But again, with the little detail you've given, I'm not sure I'm addressing *your8 issue. –  Nov 03 '17 at 02:28

2 Answers2

1

You can do it as - first iterates line in your text and then apply background color with lines. Here i used Multi-line String Literals. By using NSBackgroundColorAttributeName apply color to lines.

   @IBOutlet weak var myLabel: UILabel!

    myLabel.text = """
    GET
    MORE LIVES
    """
    myLabel.numberOfLines = 0
    let attributeString = NSMutableAttributedString(string: myLabel.text!)
    let labelText = myLabel.text!
    var lines: [String] = []
    labelText.enumerateLines { line, _ in
        lines.append(line)
    }
    print(lines)//lines in your text
    var startIndex = 0
    for value in lines {
    //Apply background color to lines
        attributeString.addAttribute(NSBackgroundColorAttributeName, value: UIColor.red, range: NSRange(location: startIndex, length: value.characters.count))
        startIndex = startIndex + value.characters.count + 1
    //startIndex will locate new line's first index
    }
    //Assign attributedText to your label
    myLabel.attributedText = attributeString
Jack
  • 13,571
  • 6
  • 76
  • 98
0

We cannot wrap the colour of a view in iOS, The possibilities are

  1. Keeping duplicate UILabels as Nathan said.
  2. Designing a canvas and and assign it to "UIImageView".

I think last possibility is the better way to implement

You can also view my GitHub repository regarding this query.

Surya
  • 546
  • 7
  • 13