简体   繁体   中英

iOS 7 Text Kit is skipping text in layout

I have a strange problem with new iOS 7 Text Kit.

I created a very simple program to demonstrate. It reads the contents of a file which contains numbers 1 - 300 in a single column. It than displays the text one "page" at a time following taps on a UITextView. (I have a single view and I just keep removing and replacing the UITextView with a new one created with the NSTextContainer).

The first page displays numbers 1 - 136. The second page should start at 137, but it doesn't. The second page starts with 155 and ends at 262. The Text Kit skipped 18 text lines. Then the third page starts at 281 (skipping 19 text lines) and displays the rest of the number through 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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