1

CTLineRefが改行の結果であるかどうかを判断しようとしています。CTFramesetterCreateFrameを使用して、すべての改行ロジックを処理しています(手動で改行を作成していません)。私はこれをどのように行うことができるかについての考えを持っていますが、それが新しい改行の結果であるかどうかを具体的に示す、CTLineRefにある種のメタデータがあることを望んでいます。

例:

オリジナル:

This is a really long line that goes on for a while.

CTFramesetterCreateFrameが改行を適用した後:

This is a really long line that
goes on for a while.

したがって、「しばらく続く」が改行の結果であるかどうかを判断したいと思います。

4

1 に答える 1

0

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呼び出しがあることを期待していました。

于 2012-10-09T18:58:43.267 に答える