NSTextField: Change Text Color at Insertion Point?

139 views
Skip to first unread message

Alexander Heinz

unread,
Feb 8, 2010, 11:22:05 AM2/8/10
to Coco...@lists.apple.com
Hello Cocoa-devs,

I've a got an out-of-the box NSTextField that I'm trying to apply various string attributes to, including colors. I'm not using the standard color panel, since the application I'm writing can only support a very limited set of colors, so I just have a menu with a list of colors. So far, the user can select text in the field, click (or press a keyboard shortcut for) one of the menu items, and the selected text will change colors (via NSText's - setTextColor: range: method.) If the field is empty, the user can also choose one of the menu items and any text he or she types will appear in the selected color.

My question is this: let's say my user has already typed some text in black (or whatever color) and now wants to change the "active color" of the text at the insertion point, such that any text he or she types appears in the new color. I've tried calling - setTextColor: range: with range: being the end of the string, but this appears to do nothing.

I'm sure this should be possible, because the standard NSColorPanel allows you to do this (as long as the field is set to use rich text, which mine is.) Google shows me a couple ways to change the color of the insertion point itself, but not the color of the text at the insertion point.

Anyone know how to do this?

Thanks in advance,
- Alex_______________________________________________

Cocoa-dev mailing list (Coco...@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/cocoa-dev-garchive-98506%40googlegroups.com

This email sent to cocoa-dev-ga...@googlegroups.com

Andreas Mayer

unread,
Feb 8, 2010, 12:11:31 PM2/8/10
to coco...@lists.apple.com

Am 08.02.2010 um 17:22 Uhr schrieb Alexander Heinz:

> My question is this: let's say my user has already typed some text
> in black (or whatever color) and now wants to change the "active
> color" of the text at the insertion point, such that any text he or
> she types appears in the new color.

I think you will have to change the typing attributes fo the
corresponding field editor.

See:

http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/TextArchitecture/Concepts/TextFieldsAndViews.html

http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/TextUILayer/Tasks/SetTextAttributes.html


Andreas

Alexander Heinz

unread,
Feb 8, 2010, 12:36:27 PM2/8/10
to Andreas Mayer, coco...@lists.apple.com
Thanks for the pointer; I wouldn't have known to look for "typing attributes." Unfortunately, while NSTextView has a - setTypingAttributes: method, the documentation for NSTextField, NSTextFieldCell, and NSText doesn't even mention "typing attributes," so I'm still stuck.

Kyle Sluder

unread,
Feb 8, 2010, 12:49:27 PM2/8/10
to Alexander Heinz, coco...@lists.apple.com, Andreas Mayer
On Mon, Feb 8, 2010 at 9:36 AM, Alexander Heinz
<ahe...@johnshopkins.edu> wrote:
> Thanks for the pointer; I wouldn't have known to look for "typing attributes." Unfortunately, while NSTextView has a - setTypingAttributes: method, the documentation for NSTextField, NSTextFieldCell, and NSText doesn't even mention "typing attributes," so I'm still stuck.

"Typing attributes" only makes sense when you're typing, so logically
it will only apply to the field editor, which is an instance of
NSTextView (even though it's typed as NSText).

--Kyle Sluder

Alexander Heinz

unread,
Feb 8, 2010, 1:10:32 PM2/8/10
to Kyle Sluder, coco...@lists.apple.com, Andreas Mayer
Oh. I knew that.

In all seriousness, thanks a lot, that's exactly what I needed to know.

- Alex

Reply all
Reply to author
Forward
0 new messages