Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing Background color on NSAttributedString

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!

like image 887
albertosh Avatar asked Mar 24 '11 18:03

albertosh


People also ask

How do I change the color of NSMutableAttributedString?

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.

How do I change the background color in Xcode?

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.


2 Answers

I think you want NSBackgroundColorAttributeName. Such as:

[mutableAttributedString addAttribute:NSBackgroundColorAttributeName value:[UIColor yellowColor] range:selectedRange];
like image 54
DenVog Avatar answered Sep 26 '22 02:09

DenVog


For Swift:

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

Helpful link - Different way to attribute a string

like image 25
Sahil_Saini Avatar answered Sep 25 '22 02:09

Sahil_Saini