Re: How to implement font bigger/smaller buttons?
Re: How to implement font bigger/smaller buttons?
- Subject: Re: How to implement font bigger/smaller buttons?
- From: Bill Bumgarner <email@hidden>
- Date: Mon, 28 Oct 2002 15:49:19 -0500
On Monday, October 28, 2002, at 02:26 PM,
email@hidden wrote:
From: Jan Van Boghout <email@hidden>
How would one do this? Is there maybe some sample code available?
To see what I mean, try the toolbar items in Mail when composing a new
message.
Has anybody ever done this?
Thanks,
Jan Van Boghout
This is a feature in Xmanview and it proved to be somewhat less
straightforward to implement than expected. In particular, the text
view already had fonts of different size and style, so it wasn't just a
case of "make everything the next size larger/smaller" and I also
wanted to do something other than just increasing it by points sizes--
i.e. bigger should mean "the next larger size according to the list of
sizes displayed in the font panel".
So, I ended up w/this code -- sort of brute force, but it works.
//
// custom font support
//
- (float) largerSizeForSize: (float) aSize;
/*" Given a font size of aSize, return the next larger size. Uses the
same list of font sizes as presented in the font panel. "*/
{
minimumFontDisplayed = NO;
if (aSize <= 8.0) return 9.0;
if (aSize <= 9.0) return 10.0;
if (aSize <= 10.0) return 11.0;
if (aSize <= 11.0) return 12.0;
if (aSize <= 12.0) return 13.0;
if (aSize <= 13.0) return 14.0;
if (aSize <= 14.0) return 18.0;
if (aSize <= 18.0) return 24.0;
if (aSize <= 24.0) return 36.0;
if (aSize <= 36.0) return 48.0;
if (aSize <= 48.0) return 64.0;
if (aSize <= 64.0) return 72.0;
if (aSize <= 72.0) return 96.0;
if (aSize <= 96.0) return 144.0;
maximumFontDisplayed = YES;
// looks odd, but everything reasonable should have been covered
above
return 288.0;
}
- (float) smallerSizeForSize: (float) aSize;
/*" Given a font size of aSize, return the next smaller size. Uses
the same list of font sizes as presented in the font panel. "*/
{
maximumFontDisplayed = NO;
if (aSize >= 288.0) return 144.0;
if (aSize >= 144.0) return 96.0;
if (aSize >= 96.0) return 72.0;
if (aSize >= 72.0) return 64.0;
if (aSize >= 64.0) return 48.0;
if (aSize >= 48.0) return 36.0;
if (aSize >= 36.0) return 24.0;
if (aSize >= 24.0) return 18.0;
if (aSize >= 18.0) return 14.0;
if (aSize >= 14.0) return 13.0;
if (aSize >= 13.0) return 12.0;
if (aSize >= 12.0) return 11.0;
if (aSize >= 11.0) return 10.0;
if (aSize >= 10.0) return 9.0;
minimumFontDisplayed = YES;
// looks odd, but everything reasonable should have been covered
above
return 8.0;
}
- (void) changeFontSize: (BOOL) largerFlag onString:
(NSMutableAttributedString *) aString;
/*" Traversess the contents of aString and makes all fonts contained
within larger or smaller depending on largerFlag. Uses the
#{-largerSizeForSize:} and #{-smallerSizeForSize:} methods to make each
font larger/smaller by an appropriate amount. Simply stepping by 1
pint is not useful at larger sizes and, as such, the logarithmic array
of font sizes as presented by the Font Panel is used. "*/
{
int index, max = [aString length];
float newFontSize;
BOOL minSizeHit = NO;
BOOL maxSizeHit = NO;
for (index = 0; index < max; index++ ) {
NSRange coverageRange;
NSFont *font = [aString attribute: NSFontAttributeName atIndex:
index effectiveRange: &coverageRange];
if (!font) continue;
index = index + coverageRange.length;
[aString removeAttribute: NSFontAttributeName range:
coverageRange];
newFontSize = largerFlag ? [self largerSizeForSize: [font
pointSize]] : [self smallerSizeForSize: [font pointSize]];
// cache min size / max size flag
if (largerFlag)
maxSizeHit = maximumFontDisplayed ? YES : maxSizeHit;
else
minSizeHit = minimumFontDisplayed ? YES : minSizeHit;
font = [NSFont fontWithName: [font fontName] size: newFontSize];
[aString addAttribute: NSFontAttributeName value: font range:
coverageRange];
}
// this will set the max/min font displayed flags to YES if ANY of
the fonts in the string hit the max/min size. This will prevent all
the fonts from becoming the same size as the size hits the limits.
Otherwise, the last font in the string would determine the max/min size
for everything (or something like that).
if (largerFlag)
maximumFontDisplayed = maxSizeHit;
else
minimumFontDisplayed = minSizeHit;
}
- (IBAction) makeFontLarger: sender;
/*" Action fired when the user requests the font size to be increased.
Stores font modifier into defaults database. "*/
{
NSUserDefaults *standardUserDefaults = [NSUserDefaults
standardUserDefaults];
[self changeFontSize: YES onString: [manView textStorage]];
[standardUserDefaults setInteger: [standardUserDefaults
integerForKey: @"FontMod"] + 1 forKey: @"FontMod"];
}
- (IBAction) makeFontSmaller: sender;
/*" Action fired when the user requests the font size to be decreased.
Stores font modifier into defaults database. "*/
{
NSUserDefaults *standardUserDefaults = [NSUserDefaults
standardUserDefaults];
[self changeFontSize: NO onString: [manView textStorage]];
[standardUserDefaults setInteger: [standardUserDefaults
integerForKey: @"FontMod"] - 1 forKey: @"FontMod"];
}
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.