iOS 7 Text Kit在布局中跳过文本

时间:2014-01-28 00:01:46

标签: ios ios7 textkit

我对新的iOS 7 Text Kit有一个奇怪的问题。

我创建了一个非常简单的程序来演示。它读取单个列中包含数字1 - 300的文件的内容。它比在UITextView上点击后一次显示文本一个“页面”。 (我只有一个视图,我只是继续使用NSTextContainer创建一个新的UITextView并删除它。)

第一页显示数字1 - 136.第二页应从137开始,但事实并非如此。第二页以155开头,结尾为262. Text Kit跳过了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>

1 个答案:

答案 0 :(得分:0)

事实证明,UITextView.textContainerInset是问题所在。当插入内容减少时,所有文本都会显示出来。我现在将它设置为全零,尽管10的工作正常。