如何使用NSString的sizeWithFont和drawInRect来训练要绘制多少字符串 [英] How to use NSString's sizeWithFont and drawInRect to workout how much of a string to draw

查看:134
本文介绍了如何使用NSString的sizeWithFont和drawInRect来训练要绘制多少字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用iOS中的CGContext绘制多个页面图像。我在我的应用程序中广泛使用了sizeWithFont和drawInRect组合。我需要做的是在多个页面中分割大量文本。我可以调整它的大小并确定它是否需要另一页,但我怎么知道在哪里砍它?我是否必须做一个丑陋的循环来逐字检查,直到找到一个完全适合页面的字符串长度,然后在那一点上切断字符串?有更聪明的方法吗?

I'm drawing multiple 'pages' of imagery using a CGContext in the iOS. I've used sizeWithFont and drawInRect combinations extensively in my app. What I need to do is split a large chunk of text across multiple pages. I can size it and work out whether or not it needs another page but how do I know where to chop it? Do I have to do an ugly loop to check word by word until I find a string length that perfectly fits the page and then chop the string at that point? Is there a smarter way?

任何想法?

谢谢。

推荐答案

NSString添加到UIKit用于绘制文本,您可以预先确定为给定字体呈现给定文本所需的确切空间量。如果将文本拆分为页面,则可以使用此方法。

The NSString additions to UIKit for drawing text, you can pre-determine the exact amount of space required to render a given text for a given font. If splitting the text into pages, you could use this method.

– sizeWithFont:constrainedToSize:lineBreakMode:

假设已知字体和换行模式,请创建一个与页面宽度相同的CGSize,并使用足够数量的高度。这将是我们约束文本的最大大小。

Assuming the font and line break mode is known, create a CGSize having the same width as your page, and use a sufficiently number for height. This would be the maximum size that we are constraining the text into.

CGSize maximumSize = CGSizeMake(pageWidth, 999999999);
CGSize expectedSize = [veryLongString sizeWithFont:theFont constrainedToSize:maximumSize lineBreakMode:theLineBreakMode];

expectedSize 会告诉我们的大小文本将采取假设它是否可以无限延伸(非常接近)。要查找所需页数,只需将总高度除以一页的高度。

expectedSize will tell us the size that the text will take assuming if it could extend vertically infinitely (well close to). To find the number of pages required, just divide the total height by the height of one page.

NSInteger totalPages = ceil(expectedSize.height / heightOfOnePage);

您还需要调整一页的高度以确保最后一行文字没有被剪掉了。为此,页面的高度应该是字体行高的倍数。假设初始页面高度为 300px ,并且字体高度为 16px ,那么将会有一些剪切为 300/16 = 18.75 这不是一个整数。

You would also want to adjust the height of one page to make sure that the last line of text doesn't get clipped. For that to happen, the height of the page should be a multiple of the font's line height. Say the initial page height is 300px, and the font-height is 16px, then there will be some clipping as 300/16 = 18.75 which is not a whole number.

NSInteger linesWithoutClipping = floor(initialPageHeight / theFont.lineHeight);
CGFloat optimalPageHeight = linesWithoutClipping * theFont.lineHeight;

取楼层值 18 并乘以字体行高 16 ,我们得到 288 的最佳页面高度,以确保没有裁剪。

Taking the floor value 18 and multiplying with the font line height 16, we get an optimal page height of 288 to ensure there's no clipping.

请注意,lineHeight是在iOS 4.0中引入的,但如果旧版本需要,你可以自己计算。

Note that lineHeight was introduced in iOS 4.0, but you could calculate it yourselves if needed for older versions.

这篇关于如何使用NSString的sizeWithFont和drawInRect来训练要绘制多少字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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