Re: "exponent" action in NSTextView subclass
Re: "exponent" action in NSTextView subclass
- Subject: Re: "exponent" action in NSTextView subclass
- From: email@hidden
- Date: Wed, 7 Mar 2007 12:01:28 +0100 (CET)
- Importance: Normal
>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].
Thanks Martin, your suggestion solves one half of my problem : now I can
select as I please, but the size is still
wrong. When I look at the final text storage in the debugger, I get a
suggestive information
(remember my example was to raise "456" inside "1234567") :
(gdb) po ts
{}
123{NSFont = "Helvetica 34.00 pt. P [] (0x01592130) fobj=0x0452ba90,
spc=9.45"; }456{
NSFont = "Helvetica 34.00 pt. P [] (0x01592130) fobj=0x0452ba90,
spc=9.45";
NSFontNameAttribute = "Helvetica 17.00 pt. P [] (0x01592210)
fobj=0x0451ecd0, spc=4.72";
NSSuperScript = 1;
}7{NSFont = "Helvetica 34.00 pt. P [] (0x01592130) fobj=0x0452ba90,
spc=9.45"; }
So the 17pt font was just added and ignored afterwards ... I can't see
how to fix this, since
"NSFont" is not an attribute ?
Below is the new code I tried.
Ewan
-(IBAction) exponent: (id) sender
{
NSRange selectedRange=[self selectedRange];
NSAttributedString* s0=[self attributedSubstringFromRange: selectedRange];
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 removeAttribute:NSFontNameAttribute range:fullRange];
[s addAttribute:NSFontNameAttribute value:newFont range:fullRange];
if( [self shouldChangeTextInRange:selectedRange replacementString:nil]
) {
NSTextStorage* ts = [self textStorage];
[ts beginEditing];
[ts replaceCharactersInRange:selectedRange withAttributedString: s];
[ts endEditing];
[self didChangeText];
}
}
_______________________________________________
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