我对新的 iOS 7 Text Kit 有一个奇怪的问题。
我创建了一个非常简单的程序来演示。它读取单列中包含数字 1 - 300 的文件的内容。它比在 UITextView 上点击后一次显示一个“页面”的文本。(我只有一个视图,我只是不断地删除 UITextView 并将其替换为使用 NSTextContainer 创建的新视图)。
第一页显示数字 1 - 136。第二页应该从 137 开始,但事实并非如此。第二页从 155 开始,到 262 结束。文本工具包跳过了 18 行文本。然后第三页从 281 开始(跳过 19 行文本)并显示到 300 的其余数字。
有人可以查看代码并告诉我我缺少什么吗?
#import "ViewController.h"
@interface ViewController ()
@property (nonatomic, strong) UITextView *textView;
@property (nonatomic, strong) NSLayoutManager *layoutManager;
@property (nonatomic, strong) UITapGestureRecognizer *tapGestureRecognizer;
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// read file into NSTextStorage
NSURL *url = [[NSBundle mainBundle] URLForResource:@"Workbook1" withExtension:@"html"];
NSTextStorage *storage = [[NSTextStorage alloc] initWithFileURL:url
options:nil
documentAttributes:nil
error:nil];
// add new NSLayoutManager to NSTextStorage
_layoutManager = [[NSLayoutManager alloc] init];
[storage addLayoutManager:_layoutManager];
// load first page
[self handleSingleTapForward];
}
-(void)handleSingleTapForward{
// crate a new NSTextContainer and add it to the NSLayoutManager
CGSize sizeX = CGSizeMake(200.0, 300.0);
NSTextContainer *textContainer = [[NSTextContainer alloc] initWithSize:sizeX ];
[_layoutManager addTextContainer:textContainer];
// remove the previous UITextView
if (_textView) {
[_textView removeFromSuperview];
}
// create new text view using the NSTextContainer for its content
CGRect rectX = CGRectMake(100, 100, 200, 300);
_textView = [[UITextView alloc] initWithFrame:rectX textContainer:textContainer];
_textView.textContainerInset = UIEdgeInsetsMake(50, 30, 50, 30);
_textView.scrollEnabled = NO;
// Set up the gesture recognizer to call handleSingleTapForward: on a single tap
_tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTapForward)];
[_tapGestureRecognizer setNumberOfTapsRequired:1];
[_textView addGestureRecognizer:_tapGestureRecognizer];
// Add the new UITextView to the main view.
[self.view addSubview:_textView];
}
@end
输入文件非常简单,如下所示(为节省空间而剪掉了):
<html><body>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
...
300
</body></html>