Changing Background color on NSAttributedString
Asked Answered
A

3

19

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!

Audio answered 24/3, 2011 at 18:50 Comment(0)
P
33

I think you want NSBackgroundColorAttributeName. Such as:

[mutableAttributedString addAttribute:NSBackgroundColorAttributeName value:[UIColor yellowColor] range:selectedRange];
Proser answered 5/12, 2012 at 22:23 Comment(2)
Works for me. iOS is the only thing I've used it with.Proser
What if I want to put a background color only the text, words and spaces, without putting bg color on the new line? ThanksGoodrich
H
2

For Swift:

attributedString.addAttribute(NSBackgroundColorAttributeName, value: yourColor, range: NSMakeRange(0, tillLocation))

Helpful link - Different way to attribute a string

Horsepowerhour answered 13/2, 2018 at 12:54 Comment(0)
S
0

The best plan would really be to draw the string on a view with a black background.

Steelworks answered 24/3, 2011 at 18:53 Comment(1)
I found out that i wanted to change the color for the NSAttributedStringView and not the string :) silly me... Thanks!Audio

© 2022 - 2024 — McMap. All rights reserved.