简体   繁体   English

目标C-核心文字,一行中的字符数?

[英]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? 给定CTLine,如何确定其中的字符数?

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. 由于我尚未测试它们,因此无法确定它们是否可以正常工作,但是值得一试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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