• 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
[SOLVED] character palette trashes attributes
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[SOLVED] character palette trashes attributes


  • Subject: [SOLVED] character palette trashes attributes
  • From: Robert Clair <email@hidden>
  • Date: Mon, 13 Dec 2004 09:58:41 -0500

"Solved" is a bit strong. Perhaps "worked around" would be better.

The problem: When inserting characters using the character palette, something
trashes the attribute dictionary removing all the attributes except for the font entry.
(Perhaps someone at Apple typed "setAttributes" where they meant "addAttributes" ???)
The result is the new text is black, no matter what color you wanted.


The solution:

subclass NSTextView

add variables:

  BOOL                      attributesLocked;
  NSMutableDictionary*      savedAttributes;

overide insertText

- (void)insertText:(id)aString
{
  savedAttributes = [self typingAttributes];
  attributesLocked = YES;
  [super insertText: aString];
  attributesLocked = NO;
}

make the text view its own delegate and implement textViewDidChangeTypingAttributes

- (void)textViewDidChangeTypingAttributes:(NSNotification *)notification
{
  NSRange  selectedRange = [self selectedRange];
  if ( attributesLocked )
    {
      if ( selectedRange.length == 0
           && selectedRange.location > 0 )
        {
          NSMutableDictionary* attributes =
            [NSMutableDictionary dictionaryWithCapacity: 3];

NSColor* fillColor =
[savedAttributes objectForKey: NSForegroundColorAttributeName];
if ( fillColor )
{
[attributes setObject: fillColor
forKey: NSForegroundColorAttributeName];
}


          NSColor* strokeColor =
            [savedAttributes objectForKey: NSStrokeColorAttributeName];
          if ( strokeColor )
            {
              [attributes setObject: strokeColor
                             forKey: NSStrokeColorAttributeName];
            }

          NSNumber* strokeWidth =
            [savedAttributes objectForKey: NSStrokeWidthAttributeName];
          if ( strokeWidth )
            {
              [attributes setObject: strokeWidth
                             forKey: NSStrokeWidthAttributeName];
            }

          [[self textStorage] addAttributes: attributes
               range: NSMakeRange( selectedRange.location -1, 1)];
        }
    }
}

For reasons that are unclear to me textViewDidChangeTypingAttributes gets called twice during
insertText. It also gets called under lots of other circumstances where you
don't want to mess with things, which is why all the locks and stuff.


If there's a better way, I'd love to here it.

Bob Clair

_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


  • Prev by Date: Re: Kill other process's warning dialog
  • Next by Date: Re: Kill other process's warning dialog
  • Previous by thread: NSMovieView seems to be disabled first time?!
  • Next by thread: Trouble reading a binary file.
  • Index(es):
    • Date
    • Thread