如何确定nsstring的哪一部分适合矩形? [英] How to determine which part of an nsstring fits into a rect?

查看:73
本文介绍了如何确定nsstring的哪一部分适合矩形?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是关于使用核心图形的PDF格式。
但是,在两页上打印字符串时,可能涉及任何类型的分页。

This is about PDF formatting using core graphics. But it could be about any type of paging when it comes to printing a string on two pages.

我需要将字符串分布在几页上。对于较小的字符串,这不是真正的问题。对于那些我使用NSString UIKit扩展 sizeWithFont 来确定全文是否适合当前页面。如果有,那么我用 drawInRect 打印它,如果没有,则将其移至下一页。

I need to spread a string over several pages. For smaller strings this is no real problem. For those I use the NSString UIKit extensions sizeWithFont to determine, whether the full text fits on the current page or not. If it does, then I print it with drawInRect and if it does not then I move it to the next page.

效果很好,但不适用于较长的字符串。在我的应用中,单个字符串(由用户提供)甚至可能比整页更长。

Works fine but is not suitable for longer strings. In my app individual strings (provided by the user) may even be longer than a full page.

当给定整个页面或页面的剩余空间时,如何确定在给定的rect中可以显示NSString的哪一部分,所以我可以切断它并在下一页上打印剩余的字符串(或至少一部分)?

When there is a full page or some remaining space of a page given, how do I determine exactly what part of the NSString can be displayed in that given rect so that I can cut it off and print the remaining string (or at least a part of it) on the next page?

我想到了一个替代方案。那就是将全文绘制到屏幕外的图形上下文中,然后将创建的图像切成适合页面自由空间的切片。但是,如果这样做,则需要将切割线恰好放置在文本行之间。坦率地说,我不确定如何确定包含图形的文本可以安全地切成两部分(或更多部分)而又不会在中间切成一行的问题。

I thought of an alternative. That is drawing the full text into an off screen graphics context and then cut the created image into slices which fit into the free space on the pages. But if I do that then I need to place the cutting line exactly between the text lines. And frankly I am not sure how to figure out where the text containing graphic can be safely cut into two (or more) parts without cutting a line of text in its middle.

我希望我表示自己可以理解,并且有人来进行救援。

I hope I expressed myself understandable and that somebody comes to the rescue.

推荐答案

在核心文本中,有一个非常相关的函数,其最后一个参数(指向CFRange的指针)是定义如下:

In Core Text, there is a very relevant function, whose last argument (a pointer to a CFRange) is defined as follows:



fitRange

返回时,包含实际适合限制大小的字符串范围。


这篇关于如何确定nsstring的哪一部分适合矩形?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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