简体   繁体   English

iOS 7文本工具包正在跳过布局中的文本

[英]iOS 7 Text Kit is skipping text in layout

I have a strange problem with new iOS 7 Text Kit. 我对新的iOS 7文本工具包有一个奇怪的问题。

I created a very simple program to demonstrate. 我创建了一个非常简单的程序来演示。 It reads the contents of a file which contains numbers 1 - 300 in a single column. 它读取单个列中包含数字1-300的文件的内容。 It than displays the text one "page" at a time following taps on a UITextView. 然后,在点击UITextView之后,它一次只显示一个“页面”文本。 (I have a single view and I just keep removing and replacing the UITextView with a new one created with the NSTextContainer). (我只有一个视图,只是不断地用UITextView删除并用NSTextContainer创建一个新的视图替换它)。

The first page displays numbers 1 - 136. The second page should start at 137, but it doesn't. 第一页显示数字1-136。第二页应该从137开始,但不是。 The second page starts with 155 and ends at 262. The Text Kit skipped 18 text lines. 第二页开始于155,结束于262。文本工具包跳过了18个文本行。 Then the third page starts at 281 (skipping 19 text lines) and displays the rest of the number through 300. 然后,第三页从281开始(跳过19个文本行),并显示其余的数字,直到300。

Can someone look at the code and tell me what I'm missing? 有人可以看一下代码,告诉我我所缺少的吗?

#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

The input file is very simple and looks like the following (clipped to save space): 输入文件非常简单,如下所示(为了节省空间而已):

<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>

It turns out that it was the UITextView.textContainerInset that was the problem. 原来是问题出在UITextView.textContainerInset上。 When the insets are reduced all of the text shows up. 减少插图后,所有文字都会显示出来。 I now have it set to all zeros although 10's work fine as well. 我现在将其设置为全零,尽管10也可以正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM