如何获取在CTFrame上绘制的文本的实际高度 [英] How to get the real height of text drawn on a CTFrame

查看:484
本文介绍了如何获取在CTFrame上绘制的文本的实际高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

有什么办法吗?

更新

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

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

我在我的项目上使用ARC:

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);

推荐答案

您可以使用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.

这篇关于如何获取在CTFrame上绘制的文本的实际高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆