如何获取 CTFrame 上绘制的文本的真实高度

发布于 2024-12-19 14:30:29 字数 823 浏览 1 评论 0原文

我有一定数量的文本填充了一些 CTFrame (多个)。为了创建所有框架(每一页一个),我填充一个框架,使用 CTFrameGetVisibleStringRange 获取不适合该框架的文本,并重复此过程,直到处理完所有文本。

在除最后一个框架之外的所有框架上,文本占据页面的相同高度。在最后一帧上,我想知道文本占据的真实高度,以知道我可以从哪里开始绘制更多文本。

有什么办法可以做到这一点吗?

更新

根据评论的要求,这是我使用 @omz 建议的解决方案:

我在我的项目中使用 ARC:

CTFrameRef locCTFrame = (__bridge CTFrameRef)ctFrame;

//Save CTLines
lines = (NSArray *) ((__bridge id)CTFrameGetLines(locCTFrame));

//Get line origins
CGPoint lOrigins[MAXLINESPERPAGE];
CTFrameGetLineOrigins(locCTFrame, CFRangeMake(0, 0), lOrigins);
CGFloat colHeight = self.frame.size.height;

//Save the amount of the height used by text
percentFull = ((colHeight - lOrigins[[lines count] - 1].y) / colHeight);

I have a certain amount of text that fill some CTFrame (more than one). To create all frames (one for each page), I'm filling one frame, getting the text that didn't fitted the frame using CTFrameGetVisibleStringRange and repeating this process until all text is processed.

On all frames, except the last, the text occupies the same height of page. On last frame I'd like to know the real height the text occupies, to know where I could start drawing more text.

Is there any way to do this?

UPDATE

As requested on comments, here's my solution using @omz 's suggestion:

I'm using ARC on my project:

CTFrameRef locCTFrame = (__bridge CTFrameRef)ctFrame;

//Save CTLines
lines = (NSArray *) ((__bridge id)CTFrameGetLines(locCTFrame));

//Get line origins
CGPoint lOrigins[MAXLINESPERPAGE];
CTFrameGetLineOrigins(locCTFrame, CFRangeMake(0, 0), lOrigins);
CGFloat colHeight = self.frame.size.height;

//Save the amount of the height used by text
percentFull = ((colHeight - lOrigins[[lines count] - 1].y) / colHeight);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

韬韬不绝 2024-12-26 14:30:29
+ (CGSize)measureFrame:(CTFrameRef)frame
{
    // 1. measure width
    CFArrayRef  lines       = CTFrameGetLines(frame);
    CFIndex     numLines    = CFArrayGetCount(lines);
    CGFloat     maxWidth    = 0;

    for(CFIndex index = 0; index < numLines; index++)
    {
        CTLineRef   line = (CTLineRef) CFArrayGetValueAtIndex(lines, index);
        CGFloat     ascent, descent, leading, width;

        width = CTLineGetTypographicBounds(line, &ascent,  &descent, &leading);

        if(width > maxWidth)
            maxWidth = width;
    }

    // 2. measure height
    CGFloat ascent, descent, leading;

    CTLineGetTypographicBounds((CTLineRef) CFArrayGetValueAtIndex(lines, 0), &ascent,  &descent, &leading);
    CGFloat firstLineHeight = ascent + descent + leading;

    CTLineGetTypographicBounds((CTLineRef) CFArrayGetValueAtIndex(lines, numLines - 1), &ascent,  &descent, &leading);
    CGFloat lastLineHeight  = ascent + descent + leading;

    CGPoint firstLineOrigin;
    CTFrameGetLineOrigins(frame, CFRangeMake(0, 1), &firstLineOrigin);

    CGPoint lastLineOrigin;
    CTFrameGetLineOrigins(frame, CFRangeMake(numLines - 1, 1), &lastLineOrigin);

    CGFloat textHeight = ABS(firstLineOrigin.y - lastLineOrigin.y) + firstLineHeight + lastLineHeight;

    return CGSizeMake(maxWidth, textHeight);
}
+ (CGSize)measureFrame:(CTFrameRef)frame
{
    // 1. measure width
    CFArrayRef  lines       = CTFrameGetLines(frame);
    CFIndex     numLines    = CFArrayGetCount(lines);
    CGFloat     maxWidth    = 0;

    for(CFIndex index = 0; index < numLines; index++)
    {
        CTLineRef   line = (CTLineRef) CFArrayGetValueAtIndex(lines, index);
        CGFloat     ascent, descent, leading, width;

        width = CTLineGetTypographicBounds(line, &ascent,  &descent, &leading);

        if(width > maxWidth)
            maxWidth = width;
    }

    // 2. measure height
    CGFloat ascent, descent, leading;

    CTLineGetTypographicBounds((CTLineRef) CFArrayGetValueAtIndex(lines, 0), &ascent,  &descent, &leading);
    CGFloat firstLineHeight = ascent + descent + leading;

    CTLineGetTypographicBounds((CTLineRef) CFArrayGetValueAtIndex(lines, numLines - 1), &ascent,  &descent, &leading);
    CGFloat lastLineHeight  = ascent + descent + leading;

    CGPoint firstLineOrigin;
    CTFrameGetLineOrigins(frame, CFRangeMake(0, 1), &firstLineOrigin);

    CGPoint lastLineOrigin;
    CTFrameGetLineOrigins(frame, CFRangeMake(numLines - 1, 1), &lastLineOrigin);

    CGFloat textHeight = ABS(firstLineOrigin.y - lastLineOrigin.y) + firstLineHeight + lastLineHeight;

    return CGSizeMake(maxWidth, textHeight);
}
孤凫 2024-12-26 14:30:29

您可以使用 CTFrameGetLineOrigins 获取框架中最后一行的行原点,也可以使用 CTFramesetterSuggestFrameSizeWithConstraints 函数获取给定范围的矩形框架的大小。如果您使用非矩形路径来设置实际帧,后者将不起作用。

You could either get the line origin of the last line in the frame with CTFrameGetLineOrigins or use the CTFramesetterSuggestFrameSizeWithConstraints function to get the size of a rectangular frame for a given range. The latter wouldn't work if you use non-rectangular paths for setting the actual frames though.

深海夜未眠 2024-12-26 14:30:29

使用CTLineGetTypgraphicBounds。

Use CTLineGetTypographicBounds.

爱的十字路口 2024-12-26 14:30:29

我认为 user1021430 说高度计算不正确是正确的。

为了获得正确的高度,您需要获取第一条线的顶部(原点 + 第一次上升)和最后一条线的底部(原点 - 下降),然后将两者相减得到实际高度。

CGSize
MeasureTextWithinFrame(
    CTFrameRef      frame)
{
    CGSize textSize = CGSizeMake(0.0f, 0.0f);
    
    CFArrayRef  lines       = CTFrameGetLines(frame);
    CFIndex     numLines    = CFArrayGetCount(lines);

    // if there is at least one line
    if (numLines > 0) {
    
        // measure width
        for (CFIndex index = 0; index < numLines; index++)  {
            CTLineRef line = (CTLineRef) CFArrayGetValueAtIndex(lines, index);
            CGFloat ascent, descent, leading, width;
            width = CTLineGetTypographicBounds(line, &ascent,  &descent, &leading);
            if (width > textSize.width)
                textSize.width = width;
        }
    
        // measure height
        CGFloat firstAscent, firstDescent, firstLeading;
        CTLineGetTypographicBounds((CTLineRef) CFArrayGetValueAtIndex(lines, 0), &firstAscent,  &firstDescent, &firstLeading);

        CGPoint firstLineOrigin;
        CTFrameGetLineOrigins(frame, CFRangeMake(0, 1), &firstLineOrigin);

        CGFloat lastAscent, lastDescent, lastLeading;
        CTLineGetTypographicBounds((CTLineRef) CFArrayGetValueAtIndex(lines, numLines - 1), &lastAscent,  &lastDescent, &lastLeading);
        
        CGPoint lastLineOrigin;
        CTFrameGetLineOrigins(frame, CFRangeMake(numLines - 1, 1), &lastLineOrigin);
        
        float top = firstLineOrigin.y + firstAscent;
        float bottom = lastLineOrigin.y - lastDescent;
        textSize.height = ABS(top - bottom);
    }
    
    return textSize;
}

I think user1021430 is correct in saying that the height is not correctly calculated.

To get the correct height, you wan to get the top of the first line (origin + first ascent) and the bottom of the last line (origin - descent), and then subtract the two come up with the actual height.

CGSize
MeasureTextWithinFrame(
    CTFrameRef      frame)
{
    CGSize textSize = CGSizeMake(0.0f, 0.0f);
    
    CFArrayRef  lines       = CTFrameGetLines(frame);
    CFIndex     numLines    = CFArrayGetCount(lines);

    // if there is at least one line
    if (numLines > 0) {
    
        // measure width
        for (CFIndex index = 0; index < numLines; index++)  {
            CTLineRef line = (CTLineRef) CFArrayGetValueAtIndex(lines, index);
            CGFloat ascent, descent, leading, width;
            width = CTLineGetTypographicBounds(line, &ascent,  &descent, &leading);
            if (width > textSize.width)
                textSize.width = width;
        }
    
        // measure height
        CGFloat firstAscent, firstDescent, firstLeading;
        CTLineGetTypographicBounds((CTLineRef) CFArrayGetValueAtIndex(lines, 0), &firstAscent,  &firstDescent, &firstLeading);

        CGPoint firstLineOrigin;
        CTFrameGetLineOrigins(frame, CFRangeMake(0, 1), &firstLineOrigin);

        CGFloat lastAscent, lastDescent, lastLeading;
        CTLineGetTypographicBounds((CTLineRef) CFArrayGetValueAtIndex(lines, numLines - 1), &lastAscent,  &lastDescent, &lastLeading);
        
        CGPoint lastLineOrigin;
        CTFrameGetLineOrigins(frame, CFRangeMake(numLines - 1, 1), &lastLineOrigin);
        
        float top = firstLineOrigin.y + firstAscent;
        float bottom = lastLineOrigin.y - lastDescent;
        textSize.height = ABS(top - bottom);
    }
    
    return textSize;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文