Re: what is [textStorage paragraphs]???
Re: what is [textStorage paragraphs]???
- Subject: Re: what is [textStorage paragraphs]???
- From: Douglas Davidson <email@hidden>
- Date: Mon, 23 Jan 2006 09:55:04 -0800
On Jan 19, 2006, at 10:32 PM, Nik Youdale wrote:
I'm playing around with textView's and I'm wandering what sort of
objects are returned by [textStorage paragraphs] method. I know
that it returns an array, but what kind of objects are in the
array? and more importantly, how can i get the string of the
paragraph?
-paragraphs is a scripting-related method; it returns an array of
text storages, one for each paragraph of the original. That's really
designed for scripting, and usually isn't particularly convenient or
efficient for other purposes. Here's some sample code I showed at
WWDC a few years ago that demonstrates iteration by paragraphs, as
well as character-to-glyph index conversion, location of lines of
text in a text view, etc.
Douglas Davidson
#import <Cocoa/Cocoa.h>
@interface BubbleTextView : NSTextView {
}
@end
@implementation BubbleTextView
#define kImageExtraXDirty 20.0
#define kImageExtraYDirty 20.0
- (void)awakeFromNib {
// Set up a paragraph style with extra room between the
paragraphs for the bubble edges.
NSMutableParagraphStyle *paragraphStyle = [[NSParagraphStyle
defaultParagraphStyle] mutableCopy];
[paragraphStyle setParagraphSpacingBefore:kImageExtraYDirty];
[paragraphStyle setParagraphSpacing:kImageExtraYDirty];
[self setDefaultParagraphStyle:paragraphStyle];
[self setTypingAttributes:[NSDictionary
dictionaryWithObjectsAndKeys:paragraphStyle,
NSParagraphStyleAttributeName, [NSFont userFontOfSize:24.0],
NSFontAttributeName, nil]];
// Make sure there's some extra room around the text container,
too.
[self setTextContainerInset:NSMakeSize(kImageExtraXDirty,
kImageExtraYDirty)];
[paragraphStyle release];
}
- (void)setNeedsDisplayInRect:(NSRect)rect avoidAdditionalLayout:
(BOOL)flag {
// Make sure that the extra space around each paragraph needed
for the bubble is marked as needing display.
[super setNeedsDisplayInRect:NSInsetRect(rect, -
kImageExtraXDirty, -kImageExtraYDirty) avoidAdditionalLayout:flag];
}
- (void)drawBubbleAroundTextInRect:(NSRect)rect {
// This is a significant simplification for demo purposes.
// Real bubble drawing would need to be sized in height as well
as in width.
// This three-part image drawing routine shows the basic
techniques involved.
// Implementation of the fully sizable nine-part image drawing
is left as an exercise to the reader...
NSImage *lEndCap = [NSImage imageNamed:@"BubbleL"], *rEndCap =
[NSImage imageNamed:@"BubbleR"];
NSColor *fillColor = [NSColor colorWithPatternImage:[NSImage
imageNamed:@"BubbleC"]];
NSSize imageSize = [lEndCap size];
NSPoint compositePoint;
NSRect fillRect;
rect = NSIntegralRect(rect);
compositePoint.x = NSMinX(rect) - imageSize.width;
compositePoint.y = NSMaxY(rect) + floor(((imageSize.height -
NSHeight(rect)) / 2.0) + 0.5);
[lEndCap compositeToPoint:compositePoint
operation:NSCompositeSourceOver];
compositePoint.x = NSMaxX(rect);
[rEndCap compositeToPoint:compositePoint
operation:NSCompositeSourceOver];
fillRect = NSMakeRect(NSMinX(rect), compositePoint.y -
imageSize.height, NSWidth(rect), imageSize.height);
[[NSGraphicsContext currentContext] setPatternPhase:[self
convertPoint:fillRect.origin toView:nil]];
[fillColor set];
NSRectFillUsingOperation(fillRect, NSCompositeSourceOver);
}
- (void)drawViewBackgroundInRect:(NSRect)rect {
NSLayoutManager *layoutManager = [self layoutManager];
NSPoint containerOrigin = [self textContainerOrigin];
NSRange glyphRange, charRange, paragraphCharRange,
paragraphGlyphRange, lineGlyphRange;
NSRect paragraphRect, lineUsedRect;
// Draw the background first, before the bubbles.
[super drawViewBackgroundInRect:rect];
// Convert from view to container coordinates, then to the
corresponding glyph and character ranges.
rect.origin.x -= containerOrigin.x;
rect.origin.y -= containerOrigin.y;
glyphRange = [layoutManager glyphRangeForBoundingRect:rect
inTextContainer:[self textContainer]];
charRange = [layoutManager
characterRangeForGlyphRange:glyphRange actualGlyphRange:NULL];
// Iterate through the character range, paragraph by paragraph.
for (paragraphCharRange = NSMakeRange(charRange.location, 0);
NSMaxRange(paragraphCharRange) < NSMaxRange(charRange);
paragraphCharRange = NSMakeRange(NSMaxRange(paragraphCharRange), 0)) {
// For each paragraph, find the corresponding character and
glyph ranges.
paragraphCharRange = [[[self textStorage] string]
paragraphRangeForRange:paragraphCharRange];
paragraphGlyphRange = [layoutManager
glyphRangeForCharacterRange:paragraphCharRange
actualCharacterRange:NULL];
paragraphRect = NSZeroRect;
// Iterate through the paragraph glyph range, line by line.
for (lineGlyphRange = NSMakeRange
(paragraphGlyphRange.location, 0); NSMaxRange(lineGlyphRange) <
NSMaxRange(paragraphGlyphRange); lineGlyphRange = NSMakeRange
(NSMaxRange(lineGlyphRange), 0)) {
// For each line, find the used rect and glyph range,
and add the used rect to the paragraph rect.
lineUsedRect = [layoutManager
lineFragmentUsedRectForGlyphAtIndex:lineGlyphRange.location
effectiveRange:&lineGlyphRange];
paragraphRect = NSUnionRect(paragraphRect, lineUsedRect);
}
// Convert back from container to view coordinates, then
draw the bubble.
paragraphRect.origin.x += containerOrigin.x;
paragraphRect.origin.y += containerOrigin.y;
[self drawBubbleAroundTextInRect:paragraphRect];
}
}
@end
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden