CTLineGetStringRange(CTLineRef)を使用することになりました。わかりやすくするために。これにより、CTLineRefが表すバッキング文字列内の場所が返されます。その後、CTLineGetStringRangeによって返されたCFRange.locationと私の回線の場所との単純な比較でした。YMMVは、特定の回線の場所を取得する方法によって異なります。この情報を取得するために使用する専門クラスがあります(詳細は以下を参照)。
ここにいくつかのコードがあります:
NSUInteger totalLines = 100; // Total number of lines in document.
NSUInteger numVisibleLines = 30; // Number of lines that can fit in the view port.
NSUInteger fromLineNumber = 0; // 0 indexed line number. Add + 1 to get the actual line number
NSUInteger numFormatChars = [[NSString stringWithFormat:@"%d", totalLines] length];
NSString *numFormat = [NSString stringWithFormat:@"%%%dd\n", numFormatChars];
NSMutableString *string = [NSMutableString string];
NSArray *lines = (NSArray *)CTFrameGetLines(textFrame);
for (NSUInteger i = 0; i < numVisibleLines; ++i)
{
CTLineRef lineRef = (CTLineRef)[lines objectAtIndex:i];
CFRange range = CTLineGetStringRange(lineRef);
// This is the object I was referring to that provides me with a line's
// meta-data. The _document is an instance of a specialized class I use to store
// meta-data about the document which includes where keywords, variables,
// numbers, etc. are located within the document. (fyi, this is for a text editor)
NSUInteger lineLocation = [_document indexOfLineNumber:fromLineNumber];
// Append the line number.
if (lineLocation == range.location)
{
[string appendFormat:numFormat, actualLineNumber];
actualLineNumber++;
fromLine++;
}
// This is a continuation of a previous line (wrapped line).
else
{
[string appendString:@"\n"];
}
}
参考までに、CTLineGetStringRange API呼び出しが存在することをすでに知っていたため、回答はありませんでした。特定のCTLineRefが前の行の続きであるかどうかを示すブール値または何かを提供するAPI呼び出しがあることを期待していました。