17

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!

albertosh
  • 2,416
  • 7
  • 25
  • 32

3 Answers3

32

I think you want NSBackgroundColorAttributeName. Such as:

[mutableAttributedString addAttribute:NSBackgroundColorAttributeName value:[UIColor yellowColor] range:selectedRange];
u2Fan
  • 1,161
  • 10
  • 15
DenVog
  • 4,226
  • 3
  • 43
  • 72
1

For Swift:

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

Helpful link - Different way to attribute a string

pkamb
  • 33,281
  • 23
  • 160
  • 191
Sahil_Saini
  • 396
  • 2
  • 15
0

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

Caleb
  • 124,013
  • 19
  • 183
  • 272
  • I found out that i wanted to change the color for the NSAttributedStringView and not the string :) silly me... Thanks! – albertosh Mar 24 '11 at 20:00