Objective c 确定CTLine何时是换行的结果

Objective c 确定CTLine何时是换行的结果,objective-c,core-text,Objective C,Core Text,我试图确定CTLineRef是否是换行的结果。我使用CTFramesetterCreateFrame来处理所有的换行逻辑,而不是手动创建换行。我对如何做到这一点有一个想法,但我希望CTLineRef上有某种类型的元数据,专门说明它是否是新换行的结果 例如: 原件: This is a really long line that goes on for a while. CTFramesetterCreateFrame应用换行后: This is a really long line that

我试图确定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.

因此,我想确定“持续一段时间”是否是换行的结果。

我最终使用了CTLineGetStringRangeCTLineRef。只是为了澄清;这将返回支持字符串中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调用。我希望有一个API调用能为我提供一个布尔值或一些指示特定CTLineRef是否是前一行的延续的东西。

您可以使用CTLINEGETSTRINGCTLINEREF获得范围