Help with colored text

turncoat

Registered
Hi, I'm new to Cocoa programming, and I have some questions about coloring the text in my new program.

My application has a text window with RTDF support, I also have a color well with controls for it, and two buttons which do nothing.

I want one button to take the color from the color well, and make whatever text is selected turn to that color.

I also want to the other button to set the color in the color well to the background color of the text view.

If you know how to do either of these, please expliain how.

* On a side note, if anyone knows where I can find/learn about some code to make a word counter, I'd be appreciative also. ;)
 
ok, getting the background is easy.

set up an action for the button, then get the text view's background color:

NSColor *acolor=[textView backgroundColor];

then set the colorwell's color to acolor:

[colorwell setColor:acolor];

Setting the text color isn't difficult, but you need to know the range of the text you're changing colors. I'll use, in this example, the entire text of the text view. I'm sure you're not wanting to do this, but it's an easy example.

set up an action for that button, then get the color well's color:

NSColor *acolor=[colorwell color];

[textView setTextColor:acolor forRange:NSMakeRange(0,[[textView textStorage]length])];

...and that'll change the text color for the entire text view.

Now, if you're wanting to change the color of the selected text, that isn't too difficult. You just need to add/change the above to:

NSRange selectedRange=[textView selectedRange];

then change the text view stuff to:

[textView setTextColor:acolor forRange:selectedRange];

Now, if you're wanting to change the color in any other way, you'll have to figure out the range to use on your own.
 
Originally posted by Darkshadow
ok, getting the background is easy.

set up an action for the button, then get the text view's background color:

NSColor *acolor=[textView backgroundColor];

then set the colorwell's color to acolor:

[colorwell setColor:acolor];

Setting the text color isn't difficult, but you need to know the range of the text you're changing colors. I'll use, in this example, the entire text of the text view. I'm sure you're not wanting to do this, but it's an easy example.

set up an action for that button, then get the color well's color:

NSColor *acolor=[colorwell color];

[textView setTextColor:acolor forRange:NSMakeRange(0,[[textView textStorage]length])];

...and that'll change the text color for the entire text view.

Now, if you're wanting to change the color of the selected text, that isn't too difficult. You just need to add/change the above to:

NSRange selectedRange=[textView selectedRange];

then change the text view stuff to:

[textView setTextColor:acolor forRange:selectedRange];

Now, if you're wanting to change the color in any other way, you'll have to figure out the range to use on your own.

I think you wrote the first part backwards, I need to take the color FROM the well TO the background, and it looks like that code does the opposit.
 
Yes, you're right. I read your original post wrong. Sorry about that.

Just need to do the opposite, though.

NSColor *acolor=[colorwell color]

[textView setBackgroundColor:acolor];
 
Back
Top