I just wanted to know how could i change the background color of an NSAttributedString, i can see the background being white all the time, but i want to make it black.
Is that possible?? Many thanks!
The main here is to use a NSMutableAttributedString and the selector addAttribute:value:range with the attribute NSForegroundColorAttributeName to change a color of a string range: NSMutableAttributedString *attrsString = [[NSMutableAttributedString alloc] initWithAttributedString:label.
At the top select the attributes inspector. Under the section "View" there should be a spot that says "Background". click that and choose your colour.
I think you want NSBackgroundColorAttributeName. Such as:
[mutableAttributedString addAttribute:NSBackgroundColorAttributeName value:[UIColor yellowColor] range:selectedRange];
For Swift:
attributedString.addAttribute(NSBackgroundColorAttributeName, value: yourColor, range: NSMakeRange(0, tillLocation))
Helpful link - Different way to attribute a string
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With