简体   繁体   中英

Objective C - Core Text, number of characters in a line?

  • 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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM