Re: Acquiring the visible characters from a NSTextView
Re: Acquiring the visible characters from a NSTextView
- Subject: Re: Acquiring the visible characters from a NSTextView
- From: Michael McCracken <email@hidden>
- Date: Wed, 22 Jun 2005 13:44:55 -0700
I've done something similar in the past - here's what I had, although
in my case the textContainerOrigin is always 0,0.
- (NSRange)getViewableRange:(NSTextView *)tv{
NSScrollView *sv = [tv enclosingScrollView];
if(!sv) return NSMakeRange(0,0);
NSLayoutManager *lm = [tv layoutManager];
NSRect visRect = [tv visibleRect];
NSPoint tco = [tv textContainerOrigin];
visRect.origin.x -= tco.x;
visRect.origin.y -= tco.y;
NSRange glyphRange = [lm glyphRangeForBoundingRect:visRect
inTextContainer:[tv textContainer]];
NSRange charRange = [lm characterRangeForGlyphRange:glyphRange
actualGlyphRange:nil];
return charRange;
}
Again, this only gets the vertical clipping, but for that, it seems to
work pretty well.
-mike
PS, that code is in the public domain.
On 6/22/05, Douglas Davidson <email@hidden> wrote:
>
>
> On Jun 22, 2005, at 12:09 PM, Ian Archer wrote:
>
>
> I want to get a copy of the characters that are visible (from
>
> scrolling) within a NSTextView. Going through
>
> NSTextStorage->[NSMutableAttributedString mutableString]
> returns
>
> everything.
>
> Are you interested only in vertical clipping? You would ask the text view
> for its visibleRect, convert that into container coordinates (by subtracting
> the text view's textContainerOrigin), ask the layout manager for the glyph
> range for that bounding rect, and then ask the layout manager to convert
> that glyph range to a character range. The result would be the range of
> interest in the text storage.
>
> If you need to clip horizontally as well, that's a little trickier, because
> the result won't necessarily be a single range. You would need to go
> through and repeat the process line by line.
>
> Douglas Davidson
>
>
> _______________________________________________
> 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
>
>
--
Michael McCracken
UCSD CSE PhD Student
San Diego Supercomputer Center
http://www.cse.ucsd.edu/~mmccrack/
_______________________________________________
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