繁体   English   中英

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

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

我对新的iOS 7文本工具包有一个奇怪的问题。

我创建了一个非常简单的程序来演示。 它读取单个列中包含数字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>

原来是问题出在UITextView.textContainerInset上。 减少插图后,所有文字都会显示出来。 我现在将其设置为全零,尽管10也可以正常工作。

暂无
暂无

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

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