Re: Creating an NSTypeSetter Subclass
Re: Creating an NSTypeSetter Subclass
- Subject: Re: Creating an NSTypeSetter Subclass
- From: Douglas Davidson <email@hidden>
- Date: Mon, 4 Dec 2006 10:47:38 -0800
On Dec 2, 2006, at 8:54 PM, Seth Willits wrote:
Are there any examples of how to write a custom NSTypeSetter? I'm
rather lost, to be honest.
The override points, and the difficulty of the project, depend quite
a bit on what it is that you want to do. NSTypesetter and
NSATSTypesetter have many predefined override points for various
tasks, and you should look through their documentation to see whether
one or a combination of these will suit your needs. It is also
possible to write a complete custom typesetter from scratch, but I
don't really recommend it.
I've appended the code from an example I presented at WWDC 2003.
This is part of a larger example; the typesetter portion was intended
to increase the line spacing at certain points in the text where
necessary to accommodate annotations above or below the text.
If you have more specific questions, we can probably answer them.
Douglas Davidson
@interface AnnotatedTypesetter : NSATSTypesetter {
}
@implementation AnnotatedTypesetter
- (void)willSetLineFragmentRect:(NSRect *)lineFragmentRect
forGlyphRange:(NSRange)glyphRange usedRect:(NSRect *)usedRect
baselineOffset:(float *)baselineOffset {
NSLayoutManager *layout = [self layoutManager];
NSTextStorage *text = [layout textStorage];
NSString *string = [text string], *topAnnotation,
*bottomAnnotation;
NSFont *font;
NSRange charRange;
float spaceToAddAbove = 0.0, spaceToAddBelow = 0.0,
spaceForThisFont;
unsigned i;
// Convert the glyph range to a character range.
charRange = [layoutManager
characterRangeForGlyphRange:glyphRange actualGlyphRange:NULL];
// Iterate through the character range.
for (i = charRange.location; i < NSMaxRange(charRange); i =
NSMaxRange([string rangeOfComposedCharacterSequenceAtIndex:i])) {
// Calculate the space required for annotations with this font.
font = [text attribute:NSFontAttributeName atIndex:i
effectiveRange:NULL];
spaceForThisFont = floor([font
boundingRectForFont].size.height / 4.0);
// If there are annotations, note the maximum space required.
topAnnotation = [text attribute:TopAnnotationAttributeName
atIndex:i effectiveRange:NULL];
if (topAnnotation && spaceToAddAbove < spaceForThisFont)
spaceToAddAbove = spaceForThisFont;
bottomAnnotation = [text
attribute:BottomAnnotationAttributeName atIndex:i effectiveRange:NULL];
if (bottomAnnotation && spaceToAddBelow < spaceForThisFont)
spaceToAddBelow = spaceForThisFont;
}
// Adjust the line fragment accordingly.
lineFragmentRect->size.height += spaceToAddAbove + spaceToAddBelow;
usedRect->size.height += spaceToAddAbove + spaceToAddBelow;
*baselineOffset += spaceToAddAbove;
}
@end
_______________________________________________
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