• 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: getting visible character range
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: getting visible character range


  • Subject: Re: getting visible character range
  • From: Keith Blount <email@hidden>
  • Date: Mon, 6 Feb 2006 10:51:38 -0800 (PST)

Hi,

This is based on code that Douglas posted on this list
some time ago for something similar to what you want:

// Returns the range of characters that are contained
within the given rect (can be called
// for [self visibleRect], for instance). Thanks to
Douglas Davidson for posting this code
// on the Cocoa dev lists).
- (NSRange)characterRangeForRect:(NSRect)aRect
{
	NSRange glyphRange, charRange;
    NSLayoutManager *layoutManager = [self
layoutManager];
    NSTextContainer *textContainer = [self
textContainer];
    NSPoint containerOrigin = [self
textContainerOrigin];

	// Convert from view coordinates to container
coordinates
	aRect = NSOffsetRect(aRect, -containerOrigin.x,
-containerOrigin.y);

    glyphRange = [layoutManager
glyphRangeForBoundingRect:aRect
inTextContainer:textContainer];
    charRange = [layoutManager
characterRangeForGlyphRange:glyphRange
actualGlyphRange:NULL];

	return charRange;
}

(This is in assuming you are putting the method in an
NSTextView subclass - obviously you would just replace
the references to "self" to your text view if not.)

Just call [textView characterRangeForRect:[textView
visibleRect]] to get the range of characters that are
visible.

Hope that helps.
Cheers,
Keith

__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around
http://mail.yahoo.com
 _______________________________________________
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

  • Prev by Date: Re: NSSegmentedControl drawing problems in unified toolbar
  • Next by Date: Re: Put an image inside a NSButton
  • Previous by thread: Re: getting visible character range
  • Next by thread: where to go from here?
  • Index(es):
    • Date
    • Thread