1

我有一个长文本(多个屏幕页面)存储在 NSTextStorage 中。我实例化一个 NSLayoutManager() 并将 textStorage 添加到它。然后我继续添加特定大小的 NSTextContainers,直到我用完整个文本。容器也被添加到数组中。这个想法是只计算一次,所以我以后只能使用容器(页面)。

这都是模型类的一部分,在 UIPageViewController 中实例化。UIPageViewController 呈现仅包含 UITextView 并具有对模型的引用的 ViewController。UITextView 使用上述数组中的 NSTextContainer 进行实例化。

这一切都很好,问题是,它只工作一次。一旦我“翻”一页,另一页是空白的。当我在横向时,我得到两个 ViewController,它们都正确加载。同样,一旦我翻页,textView 是空的。

当我每次翻页时创建新的 NSTextStorage、NSLayoutManager 和 NSTextContainers 时,它都能完美运行,但是,这非常缓慢且效率低下。

知道可能是什么原因吗?

编辑:

这是在 UIPageViewController 创建的页面上创建 textView:

    func reloadData() {
    let rect = CGRect(
        x: view.bounds.origin.x + spacing,
        y: view.bounds.origin.y + spacing,
        width: view.bounds.width - spacing * 2,
        height: view.bounds.height - spacing * 2)
    if let textContainer = bookReader.getPage(pageIndex, frame: rect) {
        textView = UITextView(frame: rect, textContainer: textContainer)
        textView!.textAlignment = textAlignment
        textView!.editable = false
        lastSize = textView!.frame
        view.addSubview(textView!)
    } else {
        print("did't get a page")
    }
}

这是在 UIPageViewController 实例化的模型中,由上面的代码调用:

    func getPage(page: Int, frame: CGRect) -> NSTextContainer? {
    // if the screen size was changed (rotated) erase everything
    if self.lastFrame != frame {
        print("size was changed")
        chapters.removeAll()
        self.lastFrame = frame
    }

    // if the data has been already calculated
    if chapters.count > 0 {
        // find chapter and page (this part is not working correctly yet but that doesn`t really matter in this case, it just fetches wrong page in some cases)
        var chapter = 0
        var subPage = 0
        var checkedPages = 0
        for i in 0..<chapters.count {
            if page - 1 < (i + 1) * chapters[i]!.count {
                chapter = i
                subPage = page - 1 - checkedPages
                break
            } else {
                checkedPages += chapters[i]!.count
            }
        }

        print("desired page: \(page) - chapter: \(chapter), page: \(subPage)")

        // return textContainer
        if let textContainer = chapters[chapter]?[subPage] {
            print("the container is in the array")
            return textContainer
        } else {
            print("page couldn't been found")
            return nil
        }
    } else {
        // calculate the whole book

        numberOfPages = 0

        for (index, url) in htmls.enumerate() {
            let data = NSData(contentsOfURL: url)
            let attributedString = NSAttributedString(HTMLData: data, options: options, documentAttributes: nil)

            let textStorage = NSTextStorage(attributedString: attributedString)
            let layoutManager = NSLayoutManager()
            textStorage.addLayoutManager(layoutManager)

            var i = 0
            while layoutManager.textContainerForGlyphAtIndex(textStorage.length - 1, effectiveRange: nil) == nil {
                let textContainer = NSTextContainer(size: frame.size)
                layoutManager.addTextContainer(textContainer)
                if chapters[index] != nil {
                    chapters[index]![i] = textContainer
                } else {
                    chapters[index] = [i:textContainer]
                }

                i++
            }
            numberOfPages += i - 1
            print("chapter \(index) count \(chapters[index]!.count)")
        }
    }

    print("recursive call, number of pages: \(numberOfPages)")

    return getPage(page, frame: frame)
}
4

0 回答 0