0
votes

I have a question about CTLineGetStringIndexForPosition in core text.

I have a text like this one:

Question: Hello, how are you today?
Answer: I am fine thank you
Question: great day isn't it?
...

On the above text i have set attributes for each one 'Question:' word to make it tappable . I am putting all this text on an UITableview cell. I have many similar cells with questions and answers. When i touch The 'Question:' word on one of the cells the index i get is correct. I scroll down in the UITableview and then i have another text like the one before. When i tap on 'Question:' the index is not right.

This is how i implement the index.

index = CTLineGetStringIndexForPosition(line,CGPointMake(lineOrigins[i].x + pnt.x, pnt.y));

Any help appreciated!

1
What do you mean by "not right?" Have you taken into account that Core Text coordinates are upside-down from UIKit? Where is pnt coming from? Are you certain that it's aligned with the baseline lower-left corner of the text? There is very little to go on here. - Rob Napier
Thanks for correcting me. It is really strange. I am trying to fix it but i can not. Something is wrong with alignment. Actually when i get false index the text looks like this: suppose this text is in a new line "Question: Hello, how are you today?" then i break into the next line. When i tap at the right side of the words "Question: Hello, how are you today?" i get an index value but i shouldn't because there is no text after these words because i have a \n . - stefanosn
Then your CTLine likely has too many characters in it. Are you using the CTLine to actually perform the layout? - Rob Napier
I am doing this CTFrameGetLineOrigins( ctFrame, CFRangeMake(0, 0), lineOrigins);CTLineRef line = (CTLineRef)CFArrayGetValueAtIndex(lines, i); index = CTLineGetStringIndexForPosition(line,CGPointMake(lineOrigins[i].x + pnt.x, pnt.y)); - stefanosn

1 Answers

0
votes

You haven't explained where pnt comes from, but it is likely incorrect. CTLineGetStringIndexForPosition takes a position relative to each line's origin in the text space (which is generally upside down of what you probably expect). See Using CoreText and touches to create a clickable action for an example of how to approach this.