Re: "exponent" action in NSTextView subclass (SOLVED)
Re: "exponent" action in NSTextView subclass (SOLVED)
- Subject: Re: "exponent" action in NSTextView subclass (SOLVED)
- From: email@hidden
- Date: Wed, 7 Mar 2007 18:26:37 +0100 (CET)
- Importance: Normal
>@"NSFont" is an attribute. To be precise, it is the value of the
NSString constant whose name is NSFontAttributeName, which is >the
standard constant defined in <AppKit/NSAttributedString.h>, and
understood by the text system as the attribute name for the >font
attribute.
Yes. My mistake what a misspelling : I wrote "NSFontNameAttribute" instead of
"NSFontAttributeName".
>@"NSFontNameAttribute", which is not anything that is understood by the
text system
In fact it is, otherwise the compiler would have catched this
error immediately, saying
I didn't define NSFontNameAttribute (as Aaron Hillegass explains, that's
one of
the reasons Cocoa programmers use global variables like this, to avoid
misspellings). I guess it's a global constant in some other part of
the system.
For the record, I append my (rather different) final code here, which works
fine now. I noticed that it is necessary to use raiseBaseline: rather than
superscript, because for a small font, superscript height is not
high enough.
Regards,
Ewan
-(IBAction) exponent: (id) sender
{
NSRange selectedRange=[self selectedRange];
NSAttributedString* oldText=[self attributedSubstringFromRange:
selectedRange];
unsigned totalRangeLength=[oldText length];
NSMutableAttributedString* newText=[[NSMutableAttributedString alloc]
initWithString:[oldText string]];
NSRange homogeneousRange=NSMakeRange(0,0);
NSMutableDictionary* dict;
NSFont* oldFont;
NSFont* newFont;
float oldSize;
float newSize;
while (NSMaxRange(homogeneousRange)<totalRangeLength) {
dict=[NSMutableDictionary dictionary];
[dict setDictionary:[oldText attributesAtIndex:0
effectiveRange:&homogeneousRange]];
oldFont=[dict objectForKey:NSFontAttributeName];
oldSize=[oldFont pointSize];
newSize=oldSize/2;
newFont=[[NSFontManager sharedFontManager] convertFont: oldFont
toSize:newSize];
[dict setObject:newFont forKey:NSFontAttributeName];
[newText setAttributes:dict range:homogeneousRange];
}
if( [self shouldChangeTextInRange:selectedRange replacementString:nil]
) {
NSTextStorage* ts = [self textStorage];
[ts replaceCharactersInRange:selectedRange withAttributedString: newText];
[self setSelectedRange:selectedRange];
unsigned k;
int numberOfIterations=(int)(newSize-2);
for (k=1;k<=numberOfIterations;k++) {
[self raiseBaseline:nil];
}
[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