Re: Saving position in NSTextView
Okay, I think I have this kind of sorted (courtesy of this source and, of course, Google) - so if anyone else needs to do this in the future, they can do it with this code: To save position: NSPoint containerOrigin = [textViewLarge textContainerOrigin]; NSRect visibleRect = [textViewLarge visibleRect]; visibleRect.origin.x -= containerOrigin.x; // convert from view coordinates to container coordinates visibleRect.origin.y -= containerOrigin.y; NSRange visibleGlyphs = [[textViewLarge layoutManager] glyphRangeForBoundingRect:visibleRect inTextContainer:[textViewLarge textContainer]]; return visibleGlyphs; To retrieve position: [textViewLarge scrollRangeToVisible:scrollRange]; In these 'examples', of course, the NSTextView being considered is called textViewLarge. I do have one proviso though - and that is that this isn't perfect. If you can guarantee that your NSTextView will always be the same size (x&y) and that the size (or font) of the text will never change, then this should work without problems. If, however, you cannot guarantee those things then it'll be a little unreliable. Unreliable isn't good, but I don't have any better ideas so it'll have to do. After all, a bookmark within a few pages of the page you actually want marking is better than no bookmark at all. On Fri, Dec 4, 2009 at 6:02 PM, Douglas Davidson wrote: > > On Dec 4, 2009, at 9:30 AM, Pascal Harris wrote: > > I am writing an application which, amongst other things, can be used to >> read text files. These text files are rather long (could be more than 1MB), >> which isn't convenient for anyone to read in one sitting. The text files >> are not editable. I would like to be able to save the position in the text >> file so that a reader can come back to file at a later time and not have to >> hunt for the last sentence that they read. >> >> My research shows that I can do half of what I need using NSRange - using >> scrollRangeToVisible it seems that I can scroll to a given range (allowing >> the reader to resume where they left off). Sadly, I can't work out how I >> can save a range without the reader selecting text in the window first >> (hardly user friendly!). I need this to work invisibly - i.e. the user >> closes the window, or the app, and when the window is reopened Presto! the >> window contains the same view of the text as it did previously. >> > > If I understand correctly, what you want to be able to determine is the > range of text that is currently visible. This can be a bit tricky, since > depending on the arrangement of text, the visible text might not be a single > contiguous range in the document, but one way to do this is to get the text > view's visibleRect, convert it into container coordinates (by subtracting > the textContainerOrigin), ask the layout manager for > glyphRangeForBoundingRect:inTextContainer:, and convert the resulting glyph > range to a character range. > > Douglas Davidson > > ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: Saving position in NSTextView
On Dec 4, 2009, at 9:30 AM, Pascal Harris wrote: I am writing an application which, amongst other things, can be used to read text files. These text files are rather long (could be more than 1MB), which isn't convenient for anyone to read in one sitting. The text files are not editable. I would like to be able to save the position in the text file so that a reader can come back to file at a later time and not have to hunt for the last sentence that they read. My research shows that I can do half of what I need using NSRange - using scrollRangeToVisible it seems that I can scroll to a given range (allowing the reader to resume where they left off). Sadly, I can't work out how I can save a range without the reader selecting text in the window first (hardly user friendly!). I need this to work invisibly - i.e. the user closes the window, or the app, and when the window is reopened Presto! the window contains the same view of the text as it did previously. If I understand correctly, what you want to be able to determine is the range of text that is currently visible. This can be a bit tricky, since depending on the arrangement of text, the visible text might not be a single contiguous range in the document, but one way to do this is to get the text view's visibleRect, convert it into container coordinates (by subtracting the textContainerOrigin), ask the layout manager for glyphRangeForBoundingRect:inTextContainer:, and convert the resulting glyph range to a character range. Douglas Davidson ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Saving position in NSTextView
I am writing an application which, amongst other things, can be used to read text files. These text files are rather long (could be more than 1MB), which isn't convenient for anyone to read in one sitting. The text files are not editable. I would like to be able to save the position in the text file so that a reader can come back to file at a later time and not have to hunt for the last sentence that they read. My research shows that I can do half of what I need using NSRange - using scrollRangeToVisible it seems that I can scroll to a given range (allowing the reader to resume where they left off). Sadly, I can't work out how I can save a range without the reader selecting text in the window first (hardly user friendly!). I need this to work invisibly - i.e. the user closes the window, or the app, and when the window is reopened Presto! the window contains the same view of the text as it did previously. Perplexed. Any help that you can provide would be most welcome. ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com