• 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
Re: "exponent" action in NSTextView subclass (SOLVED)
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: "exponent" action in NSTextView subclass (SOLVED)


  • Subject: Re: "exponent" action in NSTextView subclass (SOLVED)
  • From: email@hidden
  • Date: Thu, 8 Mar 2007 07:26:26 +0100 (CET)
  • Importance: Normal

> I don't want to sound harsh

   Don't worry about that : it is an unexpected pleasure to have
so much feedback for a question on this list. I am more used to getting
laconic answers and leaving my simple-minded code "as is" ...


>  You
> should make the same modification to the typing attributes that you do
to the text attributes.

   I still don't quite understand what those "typing attributes" are all
about
and what they add to ordinary attributes; when I looked for the keywords
"typing attributes" in the documentation I only found items mentioning
them, not
items explaning what they were. Just where should I look ?



>even if you
> start with this example, it's hard to understand all of this code.

                  I see (perhaps wrongly) a problem with your
implementation. Suppose
the changeAttributes: method is called on an already "exponentiated" range
of text for a completely different action (say, one that colors
all characters green). Then that bit of text will be
"exponentiated" a second time, and it shouldn't ?


                                                                                   Ewan

>
> -(IBAction)exponent:(id)sender {
>      changeType = exponentChangeType;	// I'm assuming that changeType
> is a new ivar in your subclass, and exponentChangeType is one of perhaps
many enumerated values, each one describing a different type of change
>      [self changeAttributes:self];
>      [[self undoManager] setActionName:NSLocalizedString(@"Exponent",
> @"Undo title for exponent action")];
> }
>
> - (NSDictionary *)convertAttributes:(NSDictionary *)oldAttributes {
>      NSMutableDictionary *newAttributes = [NSMutableDictionary
> dictionaryWithDictionary:oldAttributes];
>      NSFont *font;
>      float fontSize;
>      if (changeType == exponentChangeType) {
>          font = [oldAttributes objectForKey:NSFontAttributeName]; if
(!font) font = [NSFont fontWithName:@"Helvetica" size:
> 12.0];	// this is the default font
>          fontSize = [font pointSize] / 2.0;
>          font = [[NSFontManager sharedFontManager] convertFont:font
> toSize:fontSize];
>          [newAttributes setObject:font forKey:NSFontAttributeName];
[newAttributes setObject:[NSNumber numberWithFloat:fontSize
> - 2.0] forKey:NSBaselineOffsetAttributeName];
>      }
>      return newAttributes;
> }
>
> For comparison, here's what it might look like if you implemented it
yourself:
>
> -(IBAction)exponent:(id)sender {
>      NSRange charRange = [self rangeForUserCharacterAttributeChange],
> range, curCharRange;
>      NSArray *ranges = [self rangesForUserCharacterAttributeChange];
unsigned i, count = [ranges count], curCharIndex;
>      if (ranges && charRange.location != NSNotFound) {
>          NSTextStorage *text = [self textStorage];
>          NSFont *font;
>          float fontSize;
>          NSMutableDictionary *typingAttributes;
>
>          if (text && charRange.length > 0 && [self
> shouldChangeTextInRanges:ranges replacementStrings:nil]) {
>              [text beginEditing];
>              for (i = 0; i < count; i++) {
>                  range = [[ranges objectAtIndex:i] rangeValue];
>                  curCharIndex = range.location;
>                  while (NSLocationInRange(curCharIndex, range)) {
>                      font = [text attribute:NSFontAttributeName
> atIndex:curCharIndex longestEffectiveRange:&curCharRange inRange:range];
>                      if (!font) font = [NSFont
> fontWithName:@"Helvetica" size:12.0];
>                      fontSize = [font pointSize] / 2.0;
>                      font = [[NSFontManager sharedFontManager]
> convertFont:font toSize:fontSize];
>                      curCharRange = NSIntersectionRange(curCharRange,
> range);
>                      [text addAttribute:NSFontAttributeName
> value:font range:curCharRange];
>                      [text addAttribute:NSBaselineOffsetAttributeName
> value:[NSNumber numberWithFloat:fontSize - 2.0] range:curCharRange];
>                      curCharIndex = NSMaxRange(curCharRange);
>                  }
>              }
>              [text endEditing];
>              [self didChangeText];
>              [[self undoManager] setActionName:NSLocalizedString
> (@"Exponent", @"Undo title for exponent action")];
>          }
>
>          // Set typing attributes
>          typingAttributes = [NSMutableDictionary
> dictionaryWithDictionary:[self typingAttributes]];
>          font = [typingAttributes objectForKey:NSFontAttributeName]; if
(!font) font = [NSFont fontWithName:@"Helvetica" size:12.0];
fontSize = [font pointSize] / 2.0;
>          font = [[NSFontManager sharedFontManager] convertFont:font
> toSize:fontSize];
>          [typingAttributes setObject:font forKey:NSFontAttributeName];
[typingAttributes setObject:[NSNumber
> numberWithFloat:fontSize - 2.0] forKey:NSBaselineOffsetAttributeName];
>          [self setTypingAttributes:typingAttributes];
>      }
> }
>
>




_______________________________________________

Cocoa-dev mailing list (email@hidden)

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: "exponent" action in NSTextView subclass (SOLVED)
      • From: Martin Wierschin <email@hidden>
  • Prev by Date: alloc init thread safe?
  • Next by Date: Command+Dragging NSStatusItem in NSStatusBar
  • Previous by thread: Re: alloc init thread safe?
  • Next by thread: Re: "exponent" action in NSTextView subclass (SOLVED)
  • Index(es):
    • Date
    • Thread