Текст искажен при использовании NSTextContainer/NSLayoutManager с UITextView

Я реализую постраничный текст, поэтому я использую NSLayoutManager и NSTextContainer для создания UITextViews для каждой страницы. Перелистывание работает нормально, но текст искажается. При установке того же NSTextStorage в ванильном UITextView он выглядит правильно:

// In the class that creates the pages
...
self.textStorage = [[NSTextStorage alloc] initWithString:string attributes:@{}];
self.layoutManager.textStorage = self.textStorage;
....

- (void)createPages{
    // Remove all current text containers
    while (self.layoutManager.textContainers.count > 0){
        [self.layoutManager removeTextContainerAtIndex:0];
    }
    NSMutableArray *textContainers = [NSMutableArray array];
    NSUInteger lastRenderedGlyph = 0;
    while (lastRenderedGlyph < self.layoutManager.numberOfGlyphs){
        NSTextContainer *textContainer = [[NSTextContainer alloc] initWithSize:self.pageSize];
        [self.layoutManager addTextContainer:textContainer];
        [textContainers addObject:textContainer];
        [self addDummyContainersForNewlinesAfterContainer:textContainer];
        NSRange range = [self.layoutManager glyphRangeForTextContainer:textContainer];
        lastRenderedGlyph = NSMaxRange(range);
    }
    self.textContainers = textContainers;
}

// In my ViewController
- (void)addTextViews{
    self.scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
    self.scrollView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    self.scrollView.backgroundColor = [UIColor lightGrayColor];
    [self.view addSubview:self.scrollView];
    CGFloat xOffset = 0;
    // Option #1 - This results in garbled text
    for (NSTextContainer *textContainer in self.coordinator.textContainers){
        break;
        CGRect frame = CGRectMake(xOffset, 0, self.coordinator.pageSize.width, self.coordinator.pageSize.height);
        xOffset = CGRectGetMaxX(frame)+1;
        self.scrollView.contentSize = CGSizeMake(xOffset, self.coordinator.pageSize.width);
        UITextView *textView = [[UITextView alloc] initWithFrame:frame textContainer:textContainer];
        textView.attributedText = self.coordinator.textStorage;
        [self.scrollView addSubview:textView];
    }
    // Option #2 - This does not result in garbled text
    UITextView *textView = [[UITextView alloc] initWithFrame:self.view.bounds];
    [self.view addSubview:textView];
    textView.attributedText = self.coordinator.textStorage;
}

Хороший текст Неверный текст


person arsenius    schedule 02.02.2016    source источник
comment
У вас есть пример проекта этого?   -  person karthikeyan    schedule 04.01.2017


Ответы (1)


оказывается забыл добавить

    [self.textStorage addLayoutManager:self.layoutManager];

Это решает проблему.

person arsenius    schedule 03.02.2016
comment
как разбивать страницы? Я получал все те же тексты на каждой странице. - person Meonardo; 29.03.2020