Want to find a string in a DTCoreTextView and scroll to it - is this correct? #168

Closed
dhoerl18 opened this Issue Apr 15, 2012 · 11 comments

Comments

Projects
None yet
3 participants
Contributor

dhoerl18 commented Apr 15, 2012

My goal is to scroll a DTCoreTextView up to place a line of text located somewhere in the view.

  • Get a NSString from the attributed string, and determine the offset into it containing the string I want to show (it's unique)
  • ask the DTCoreTextLayoutFrame for the appropriate DTCoreTextLayoutLine using "-(DTCoreTextLayoutLine *)lineContainingIndex:(NSUInteger)index"
  • ask the DTCoreTextLayoutLine for the frame of the first glyph using "- (CGRect)frameOfGlyphAtIndex:(NSInteger)index" (or get the whole search string frame using -frameOfGlyphsWithRange)
  • the returned CGRect has the appropriate offset contained in frame.origin.y, so that is where I scroll to

Is this the way to achieve my goal? If not please provide pointers. Thanks!

Collaborator

odrobnik commented Apr 15, 2012

Once you know the layout line you can simple scroll to it's frame.

Contributor

dhoerl18 commented Apr 15, 2012

OK - but the earliers steps are correct as to HOW to find the line? I'll make a wiki out of this shortly.

Collaborator

odrobnik commented Apr 15, 2012

Yes, these steps should work.

Collaborator

odrobnik commented Apr 17, 2012

I was thinking to have this as a new feature for hyperlinks. It should be fairly easy - with the steps we discussed - to determine the vertical location of a anchor tag and have the DTLinkButton scroll there instead of opening a browser.

Contributor

dhoerl commented Apr 17, 2012

We have the HTML blob - a FAQ - with questions at the top containing tags for the actual text for the Q&A. Right now another engineer is trying to figure out how to best find the location - that is, he is switching back and forth between the actual HTML and the "string" property returned by the attributed string. We haven't really got it working yet but are close.

Contributor

dhoerl18 commented Apr 19, 2012

So now have tried to make this work, its close but we cannot seem to find the correct line (using the plain string offset works but not properly), so either the desired text is near the bottom of the view or not visible at all.

I suspect the problem is that when we turn the attributed string into a string, then get an offset, that offset is not mapping well into "-(DTCoreTextLayoutLine *)lineContainingIndex:(NSUInteger)index"

A junior engineer has been doing the work not me so perhaps this is our issue. It occurred to me that if I could just walk every layout line, I could search them - but the other engineer says how about line breaks etc.

So - we're willing to do the work - but can you give us some direction on how best to find the layout line?

Collaborator

odrobnik commented Apr 19, 2012

It's faster if I implement a method on DTAttributedTextView.

It is literally

odrobnik closed this Apr 19, 2012

odrobnik reopened this Apr 19, 2012

Collaborator

odrobnik commented Apr 19, 2012

Oops, wrong key.

... It is literally as easy as getting the frame of the line or caret position and then scrollRectToVisible

Contributor

dhoerl18 commented Apr 19, 2012

Again, we have a long HTML page with a table of contents at the top that contain tags to the content. I assume a UIWebView would scroll to the proper place when the title it tapped - it works fine in web browsers (its the typical TOC at top with Q&A at the bottom). There is no caret - this is a read only view. But, how to I find the proper DTCoreTextLayoutLine to get the position? The technique of using the "index I said earlier is not working properly and returning a line that is earlier than the desired line. I know how to scroll to the "y" offset when I finally get a good frame.

Collaborator

odrobnik commented Apr 21, 2012

I implemented bcac407 which demonstrates in the Demo app how to use the link button to scroll to a named anchor.

odrobnik closed this Apr 21, 2012

Contributor

dhoerl18 commented Apr 21, 2012

You are too kind!!!

David

On Apr 21, 2012, at 10:21 AM, Oliver Drobnik wrote:

I implemented bcac407 which demonstrates in the Demo app how to use the link button to scroll to a named anchor.


Reply to this email directly or view it on GitHub:
#168 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment