Re: How to set the foreground and background colors of selected text in a field editor (NSTextView)?
Re: How to set the foreground and background colors of selected text in a field editor (NSTextView)?
- Subject: Re: How to set the foreground and background colors of selected text in a field editor (NSTextView)?
- From: Stephane Sudre <email@hidden>
- Date: Tue, 10 Apr 2012 11:29:06 -0700
Interesting problem.
I'm afraid one of the easier solutions is to use a NSTextView instead
of a NSTextField. When you set the selectedAttributes of the field
editor, I believe it will be forgotten as the field editor will try to
mimick the way the text look like when not edited, (Sure the doc says
these attributes are OK but they are for the NSTextView that will be
edited AFAIK).
Allowing rich text, subclassing NSTextField and add some methods from
NSTextView did not work so far. No obvious private API for this in the
AppKit.
On Tue, Apr 10, 2012 at 7:51 AM, Michael Crawford
<email@hidden> wrote:
> I've tried two different approaches. The first thing I tried was to make may app-delegate the delegate for my window and then implement -windowWillReturnFieldEditor:toObject:.
>
> - (void)awakeFromNib
> {
> _fieldEditor = [NSTextView new];
> [_fieldEditor setFieldEditor:YES];
>
> NSDictionary* attributes = [NSDictionary dictionaryWithObjectsAndKeys:
> [NSColor redColor], NSBackgroundColorAttributeName, /* something obvious so I can see it */
> [NSColor yellowColor], NSForegroundColorAttributeName,
> nil];
> [_fieldEditor setSelectedTextAttributes:attributes];
> }
>
> - (id)windowWillReturnFieldEditor:(NSWindow *)sender toObject:(id)client
> {
> if ( [client isKindOfClass:[NSTextField class]] )
> {
> return _fieldEditor;
> }
>
> return nil;
> }
>
> Next, I tried something I found in a blog via Google at http://www.ff00aa.com/fr/archives/2009/02/28/9969-changing-the-selected-text-s-color-for-an-nstextfield/.
>
> - (void)viewDidMoveToWindow:(NSWindow *)window
> {
> if ( window != nil )
> {
> NSDictionary* attributes = [NSDictionary dictionaryWithObjectsAndKeys:
> [NSColor redColor], NSBackgroundColorAttributeName,
> [NSColor yellowColor], NSForegroundColorAttributeName,
> nil];
>
> // object-shadow text-field
> NSTextView *fieldEditor = (NSTextView *)[window fieldEditor:YES forObject:_shadowTextField];
> [fieldEditor setSelectedTextAttributes:attributes];
>
> // vignette text-field
> fieldEditor = (NSTextView *)[window fieldEditor:YES forObject:_vignetteTextField];
> [fieldEditor setSelectedTextAttributes:attributes];
>
> // opacity text-field
> fieldEditor = (NSTextView *)[window fieldEditor:YES forObject:_opacityTextField];
> [fieldEditor setSelectedTextAttributes:attributes];
> }
> }
>
> End result: In both cases I get the default white on cyan or whatever color you call it. Anyone have a technique that works or some experience with this type of modification?
>
> -Michael
> _______________________________________________
>
> Cocoa-dev mailing list (email@hidden)
>
> 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:
>
> This email sent to email@hidden
_______________________________________________
Cocoa-dev mailing list (email@hidden)
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:
This email sent to email@hidden