• 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
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: "exponent" action in NSTextView subclass


  • Subject: Re: "exponent" action in NSTextView subclass
  • From: Martin Wierschin <email@hidden>
  • Date: Wed, 7 Mar 2007 01:58:12 -0800

-(IBAction) exponent: (id) sender
{
NSRange r=[self selectedRange];
NSAttributedString* s0=[self attributedSubstringFromRange: r];
NSRange fullRange=NSMakeRange(0,[s0 length]);
NSMutableAttributedString* s=[[NSMutableAttributedString alloc]
initWithAttributedString:s0];
[s addAttribute:NSSuperscriptAttributeName value:[NSNumber
numberWithInt:1] range:fullRange ];
NSDictionary* dict=[s0 attributesAtIndex:0 effectiveRange:NULL];
NSFont* oldFont=[dict objectForKey:NSFontAttributeName];
float oldSize=[oldFont pointSize];
float newSize=oldSize/2;
NSFont* newFont=[[NSFontManager sharedFontManager] convertFont: oldFont
toSize:newSize];
[s addAttribute:NSFontNameAttribute value:newFont range:fullRange];
[self insertText:s];
[s release];
}

Not having run any of your code, I might guess that the problem is due to your use of the "insertText:" method. You'll note that the docs for this method say:


"Programmatic modification of the text is best done by operating on the text storage directly."

You'll want to use this pattern:

if( [self shouldChangeTextInRange:selectedRange replacementString:nil] ) {
NSTextStorage* ts = [self textStorage];
// modify "ts" directly as needed (NSTextStorage is a subclass of NSMutableAttributedString)
[self didChangeText];
}


If you make lots of changes to the text storage, you'll want to group the changes with calls to [ts beginEditing] and [ts endEditing].

I hope that helps,
~Martin

_______________________________________________

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
      • From: email@hidden
References: 
 >"exponent" action in NSTextView subclass (From: email@hidden)

  • Prev by Date: "exponent" action in NSTextView subclass
  • Next by Date: NSWindowController and nib owner in document-based app
  • Previous by thread: "exponent" action in NSTextView subclass
  • Next by thread: Re: "exponent" action in NSTextView subclass
  • Index(es):
    • Date
    • Thread