• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
How to set the foreground and background colors of selected text in a field editor (NSTextView)?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

How to set the foreground and background colors of selected text in a field editor (NSTextView)?


  • Subject: How to set the foreground and background colors of selected text in a field editor (NSTextView)?
  • From: Michael Crawford <email@hidden>
  • Date: Tue, 10 Apr 2012 10:51:01 -0400

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

  • Follow-Ups:
    • Re: How to set the foreground and background colors of selected text in a field editor (NSTextView)?
      • From: Stephane Sudre <email@hidden>
  • Prev by Date: Re: How to bind to NSMutableArray that may contain zero objects?
  • Next by Date: Cannot autosave a TableView without headers
  • Previous by thread: Re: Recursive call-back block
  • Next by thread: Re: How to set the foreground and background colors of selected text in a field editor (NSTextView)?
  • Index(es):
    • Date
    • Thread