toggle new attribute in range of text (was : exponent action in NSTextView subclass)
toggle new attribute in range of text (was : exponent action in NSTextView subclass)
- Subject: toggle new attribute in range of text (was : exponent action in NSTextView subclass)
- From: email@hidden
- Date: Thu, 22 Mar 2007 07:39:52 +0100 (CET)
- Importance: Normal
>Here's what it might look like, if you have an NSTextView subclass and
use it as the sender:
>
>-(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;
>}
Yes, this deals at once with both typing attributes and attributes in a
selected text. However, if a user sets the typing attributes to
"exponential mode"
he will need to return to normal mode when he's done. As far as I can see,
your code does not provide this functionality. What is needed here is a
"toggle"
behaviour, like the "Bold" (or "Italics") menu item in TextEdit. But that
is much
more complicated to implement, is it not ? It amounts to adding a new kind
of attribute to ranges of text.
Ewan
_______________________________________________
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