• 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
Getting Rects of Chars and Chars In Rect - NSTextView
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Getting Rects of Chars and Chars In Rect - NSTextView


  • Subject: Getting Rects of Chars and Chars In Rect - NSTextView
  • From: Seth Willits <email@hidden>
  • Date: Wed, 27 Jun 2007 22:58:56 -0700

Howdy,


I need separate rects for each chunk of a character range in a text view that are drawn on separate lines. rectArrayForCharacterRange: withinSelectedCharacterRange: inTextContainer: rectCount: works great for getting the rects, but it doesn't match subranges to those rects. I tried passing each rect to glyphRangeForBoundingRect:inTextContainer: in order to get the subrange of characters that are in that rect, that gives me waaaaay too big of a range (the entire line, rather than just the few chars that are in that rect).


I figured I could easily limit the range myself (see code below), but I assume I'm just not understanding the correct way to get what I want.

Also, I need to draw the characters in the range in their exact location as they are in the text view (with the exception of some color attribute changes), but in a different view in a different window. What is the correct rect I should be grabbing to pass to NSAttributedString's drawInRect? The bounding rect? It seems to be correct.

Here's the code I'm using to grab rects and ranges:



unsigned int rectIndex, rectCount;
NSRectArray rects = [lm rectArrayForCharacterRange:charRange
                        withinSelectedCharacterRange:charRange
                        inTextContainer:tc rectCount:&rectCount];

NSMutableArray * fragmentRects = [NSMutableArray array];

for (rectIndex = 0; rectIndex < rectCount; rectIndex++) {
    NSRect rect = rects[rectIndex];
    rect.origin.x += [self textContainerInset].width;
    rect.origin.y += [self textContainerInset].height;

NSRange subglyphRange = [lm glyphRangeForBoundingRect:rects [rectIndex] inTextContainer:tc];
NSRange subcharRange = [lm characterRangeForGlyphRange:subglyphRange actualGlyphRange:nil];


// Limit subchar range to the complete char range.
// glyphRangeForBoundingRect returns the range for the whole line.
if (subcharRange.location < charRange.location) {
subcharRange.length -= (charRange.location - subcharRange.location);
subcharRange.location = charRange.location;
}


if (NSMaxRange(subcharRange) > NSMaxRange(charRange)) {
subcharRange.length -= NSMaxRange(subcharRange) - NSMaxRange (charRange);
}




Here, rect is the NSRect bounds of the range subcharRange. Is there a better way to do this?




Thanks,


-- Seth Willits



_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please 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


  • Prev by Date: Creating a custom subclass of NSController
  • Next by Date: Merging/Connecting NSBezierPaths?
  • Previous by thread: Re: Creating a custom subclass of NSController
  • Next by thread: Merging/Connecting NSBezierPaths?
  • Index(es):
    • Date
    • Thread