I am trying to display the following
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
I am trying to display the following
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
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
We cannot wrap the colour of a view in iOS, The possibilities are
I think last possibility is the better way to implement
You can also view my GitHub repository regarding this query.