开发者

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开发者_JAVA百科 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜