• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: [NSTextView firstRectForCharacterRange:] returns empty rect
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [NSTextView firstRectForCharacterRange:] returns empty rect


  • Subject: Re: [NSTextView firstRectForCharacterRange:] returns empty rect
  • From: Douglas Davidson <email@hidden>
  • Date: Mon, 6 Mar 2006 09:45:10 -0800


On Mar 6, 2006, at 3:43 AM, Tristan Leblanc wrote:

I have to frame some text items.

I think about using [NSTextView firstRectForCharacterRange:]

does anyone knows how this method works?

Actually, it just returns the location of selection rect.

Width and height are always 0, no matter the range i pass to this method...

Any guess or known bug?


Please do not use this method. This is part of the NSTextInput protocol, designed to be used only for support of text input.


Take a look at e.g. -[NSLayoutManager boundingRectForGlyphRange:inTextContainer:] or -[NSLayoutManager rectArrayForCharacterRange:withinSelectedCharacterRange:inTextContainer: rectCount:]. Keep in mind that the layout manager always uses container coordinates, which are offset from the text view's coordinates by the textContainerOrigin.

Here is some sample code from a previous WWDC that might be relevant:


/* BubbleTextView.m

Copyright (c) 2003, Apple Computer, Inc., all rights reserved.
*/

/*
IMPORTANT: This Apple software is supplied to you by Apple Computer, Inc. ("Apple") in
consideration of your agreement to the following terms, and your use, installation,
modification or redistribution of this Apple software constitutes acceptance of these
terms. If you do not agree with these terms, please do not use, install, modify or
redistribute this Apple software.


In consideration of your agreement to abide by the following terms, and subject to these
terms, Apple grants you a personal, non-exclusive license, under Apple’s copyrights in
this original Apple software (the "Apple Software"), to use, reproduce, modify and
redistribute the Apple Software, with or without modifications, in source and/or binary
forms; provided that if you redistribute the Apple Software in its entirety and without
modifications, you must retain this notice and the following text and disclaimers in all
such redistributions of the Apple Software. Neither the name, trademarks, service marks
or logos of Apple Computer, Inc. may be used to endorse or promote products derived from
the Apple Software without specific prior written permission from Apple. Except as expressly
stated in this notice, no other rights or licenses, express or implied, are granted by Apple
herein, including but not limited to any patent rights that may be infringed by your
derivative works or by other works in which the Apple Software may be incorporated.


The Apple Software is provided by Apple on an "AS IS" basis. APPLE MAKES NO WARRANTIES,
EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED WARRANTIES OF NON-INFRINGEMENT,
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS
USE AND OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS.


IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE,
REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED AND
WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE), STRICT LIABILITY OR
OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/


#import "BubbleTextView.h"

@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


  • Follow-Ups:
    • Re: [NSTextView firstRectForCharacterRange:] returns empty rect
      • From: Andy Lee <email@hidden>
References: 
 >[NSTextView firstRectForCharacterRange:] returns empty rect (From: Tristan Leblanc <email@hidden>)

  • Prev by Date: Re: Mutable/non-mutable bindings in NSTextView
  • Next by Date: Re: Issues with NSTextView and undoing attributes - Solved (I think)
  • Previous by thread: Re: [NSTextView firstRectForCharacterRange:] returns empty rect
  • Next by thread: Re: [NSTextView firstRectForCharacterRange:] returns empty rect
  • Index(es):
    • Date
    • Thread