ios - Multiple NSTextContainers do not work properly with UIPageViewController -
i have long text (multiple screen pages) stored in nstextstorage. instantiate nslayoutmanager() , add textstorage it. keep adding nstextcontainers of specific size until use whole text. containers added array. idea gets calculated once can later work containers (pages).
this part of model class, instantiated in uipageviewcontroller. uipageviewcontroller presents viewcontrollers contain uitextview , has reference model. uitextview gets instantiated using nstextcontainer array described above.
this works well, problem is, works once. once "turn" page, other page blank. when in landscape, 2 viewcontrollers , both load correctly. again, once turn page, textview empty.
it works when create new nstextstorage, nslayoutmanager , nstextcontainers every time page turned, however, that`s awefully slow , inefficient.
any idea cause?
edit:
this creating textview on page created uipageviewcontroller:
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 page") } }
and in model instantiated uipageviewcontroller, called code above:
func getpage(page: int, frame: cgrect) -> nstextcontainer? { // if screen size changed (rotated) erase if self.lastframe != frame { print("size changed") chapters.removeall() self.lastframe = frame } // if data has been calculated if chapters.count > 0 { // find chapter , page (this part not working correctly yet doesn`t matter in case, fetches wrong page in cases) var chapter = 0 var subpage = 0 var checkedpages = 0 in 0..<chapters.count { if page - 1 < (i + 1) * chapters[i]!.count { chapter = 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 in array") return textcontainer } else { print("page couldn't been found") return nil } } else { // calculate whole book numberofpages = 0 (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 = 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 += - 1 print("chapter \(index) count \(chapters[index]!.count)") } } print("recursive call, number of pages: \(numberofpages)") return getpage(page, frame: frame) }
Comments
Post a Comment