Given an index for current character, how can I determine the number of line that the selected character is at?
Given a CTLine how can I determine the number of characters in it?
Asked
Active
Viewed 525 times
0

aryaxt
- 76,198
- 92
- 293
- 442
-
Was my answer any use to you? – Joshua Jul 29 '11 at 09:15
1 Answers
1
For the first one:
int currentCharacterIndex = 12; // You define this.
CFArrayRef lines = CTFrameGetLines(frame);
int currentLine = 0;
for (CTLineRef line in lines) {
currentLine++;
CFRange range = CTLineGetStringRange(line);
if (currentCharacterIndex > range.location)
break;
}
// Current line is now the line that the currentCharacterIndex resides at
For the second one:
CFRange range = CTLineGetStringRange(line);
CFIndex length = range.length; // Number of characters
Can't be sure these work as I haven't tested them but it's worth a go.

Joshua
- 15,200
- 21
- 100
- 172