- (CGRect)boundingRectWithSize:(CGSize)size options:(NSStringDrawingOptions)options attributes:(nullableNSDictionary<NSString*,id> *)attributes context:(nullableNSStringDrawingContext*)contextNS_AVAILABLE(10_11,7_0);
The width and height constraints to apply when computing the string’s bounding rectangle.
文本绘制时的附加选项。可能取值请参考“NSStringDrawingOptions”。
context上下文。包括一些信息,例如如何调整字间距以及缩放。最终,该对象包含的信息将用于文本绘制。该参数可为 nil 。
如果 NSStringDrawingUsesLineFragmentOrigin未指定,矩形的高度将被忽略,同时使用单线绘制。(由于一个 bug,在 iOS6 中,宽度会被忽略)
声明于NSStringDrawing.
另外,关于参数(NSStringDrawingOptions)options
typedef NS_ENUM(NSInteger, NSStringDrawingOptions) {
NSStringDrawingUsesFontLeading = 1 << 1, // Uses the font leading for calculating line heights
NSStringDrawingUsesDeviceMetrics = 1 << 3, // Uses image glyph bounds instead of typographic bounds
NSStringDrawingTruncatesLastVisibleLine:
如果文本内容超出指定的矩形限制,文本将被截去并在最后一个字符后加上省略号。如果没有指定NSStringDrawingUsesLineFragmentOrigin选项,则该选项被忽略。
NSStringDrawingUsesLineFragmentOrigin:
绘制文本时使用 line fragement origin 而不是 baseline origin。
NSStringDrawingUsesFontLeading:
NSStringDrawingUsesDeviceMetrics:
Use the image glyph bounds (instead of the typographic bounds) when computing layout.
简单写了一个Demo来看看该方法的使用,并比较了一下各个options的不同,首先是代码:
NSAttributedString *attrStr = [[NSAttributedString alloc] initWithString:textView.text];
textView.attributedText = attrStr;
NSRange range = NSMakeRange(0, attrStr.length);
CGSize textSize = [textView.text boundingRectWithSize:textView.bounds.size // 用于计算文本绘制时占据的矩形块
options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading // 文本绘制时的附加选项
context:nil].size; // context上下文。包括一些信息,例如如何调整字间距以及缩放。该对象包含的信息将用于文本绘制。该参数可为nil
NSLog(@"w = %f", textSize.width);
NSLog(@"h = %f", textSize.height);
NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading
2013-09-02 21:04:47.470 BoudingRect_i7_Demo[3532:a0b] w = 322.171875
2013-09-02 21:04:47.471 BoudingRect_i7_Demo[3532:a0b] h = 138.000015
2013-09-02 17:35:40.547 BoudingRect_i7_Demo[1871:a0b] w = 318.398438
2013-09-02 17:35:40.549 BoudingRect_i7_Demo[1871:a0b] h = 69.000000
2013-09-02 17:37:38.398 BoudingRect_i7_Demo[1902:a0b] w = 1523.408203
2013-09-02 17:37:38.400 BoudingRect_i7_Demo[1902:a0b] h = 13.800000
NSStringDrawingUsesFontLeading // Uses the font leading for calculating line heights
2013-09-02 17:40:45.903 BoudingRect_i7_Demo[1932:a0b] w = 1523.408203
2013-09-02 17:40:45.905 BoudingRect_i7_Demo[1932:a0b] h = 13.800000
NSStringDrawingUsesDeviceMetrics // Uses image glyph bounds instead of typographic bounds
2013-09-02 17:42:03.283 BoudingRect_i7_Demo[1956:a0b] w = 1523.408203
2013-09-02 17:42:03.284 BoudingRect_i7_Demo[1956:a0b] h = 13.800000
如果为NSStringDrawingTruncatesLastVisibleLine或者NSStringDrawingUsesDeviceMetric,那么计算文本尺寸时将以每个字或字形为单位来计算。
如果为NSStringDrawingUsesFontLeading则以字体间的行距(leading,行距:从一行文字的底部到另一行文字底部的间距。)来计算。
各个参数是可以组合使用的,如NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingTruncatesLastVisibleLine。
根据该方法我调整了一下Reader的分页方法:(主要是将被iOS7 Deprecated的sizeWithFont:constrainedToSize:lineBreakMode:方法改成了 boudingRectWithSize:options:attributes:context:方法来计算文本尺寸)
/* 获取Settings中设定好的字体(主要是获取字体大小) */
static const CGFloat textScaleFactor = 1.; // 设置文字比例
NSString *textStyle = [curPageView.textView tkd_textStyle]; // 设置文字样式
NSLog(@"paging: %@", preferredFont_.fontDescriptor.fontAttributes); // 在控制台中输出字体的属性字典
NSUInteger height = (int)self.view.bounds.size.height - 40.0; // 页面的高度
NSDictionary *dic = preferredFont_.fontDescriptor.fontAttributes;
options:NSStringDrawingUsesLineFragmentOrigin
NSLog(@"w = %f", totalTextSize.width);
NSLog(@"h = %f", totalTextSize.height);
if (totalTextSize.height < height) {
charsPerPage_ = [bookItem.content length]; // 设定每页的字符数
textLength_ = [bookItem.content length]; // 设定文本总长度
/* 计算理想状态下的页面数量和每页所显示的字符数量,用来作为参考值用 */
textLength_ = [bookItem.content length]; // 文本的总长度
NSUInteger referTotalPages = (int)totalTextSize.height / (int)height + 1; // 理想状态下的总页数
NSUInteger referCharactersPerPage = textLength_ / referTotalPages; // 理想状态下每页的字符数
NSLog(@"textLength = %d", textLength_);
NSLog(@"referTotalPages = %d", referTotalPages);
NSLog(@"referCharactersPerPage = %d", referCharactersPerPage);
/* 根据referCharactersPerPage和text view的高度开始动态调整每页的字符数 */
// 如果referCharactersPerPage过大,则直接调整至下限值,减少调整的时间
if (referCharactersPerPage > 1000) {
referCharactersPerPage = 1000;
// 获取理想状态下的每页文本的范围和pageText及其尺寸
NSString *pageText = [bookItem.content.string substringWithRange:range]; // 获取该范围内的文本
NSRange ptrange = NSMakeRange(0, pageText.length);
CGSize pageTextSize = [pageText boundingRectWithSize:curPageView.textView.bounds.size
options:NSStringDrawingUsesLineFragmentOrigin
// 若pageText超出text view的显示范围,则调整referCharactersPerPage
NSLog(@"height = %d", height);
while (pageTextSize.height > height) {
NSLog(@"pageTextSize.height = %f", pageTextSize.height);
referCharactersPerPage -= 2; // 每页字符数减2
range = NSMakeRange(0, referCharactersPerPage); // 重置每页字符的范围
CGSize pageTextSize = [pageText boundingRectWithSize:curPageView.textView.bounds.size
options:NSStringDrawingUsesLineFragmentOrigin
pageText = [bookItem.content.string substringWithRange:range]; // 重置pageText
pageTextSize = [pageText boundingRectWithSize:curPageView.textView.bounds.size
options:NSStringDrawingUsesLineFragmentOrigin
context:nil].size; // 获取pageText的尺寸
// 根据调整后的referCharactersPerPage设定好charsPerPage_
charsPerPage_ = referCharactersPerPage;
NSLog(@"cpp: %d", charsPerPage_);
totalPages_ = (int)bookItem.content.length / charsPerPage_ + 1;
NSLog(@"ttp: %d", totalPages_);
charsOfLastPage_ = textLength_ - (totalPages_ - 1) * charsPerPage_;
NSLog(@"colp: %d", charsOfLastPage_);
重要的是,由于该方法计算文本的尺寸更为准确,所以可以使得分页后页与页之间的连贯性好了很多,而且每页的空间利用率都提高了很多,每页的文字几乎铺满了整个页面。