1

I am given a large HTML content (let's say a huge blog post). The objective is to render this HTML content into multiple pages (paginated application). For clarity, instead of vertical scrolling in a UIWebView, the content is split like a book (i.e. you turn the pages to move to the rest of the content, instead of scrolling down in a typical UIWebView).

I looked up on SO for similar posts. Unfortunately, these posts haven't been so useful in my case. For reference, here are the similar SO posts:

  1. Split html string into multiple pages
  2. How to divide the webview content in multiple pages
  3. Present html content as dynamic "pages"
  4. External reference: https://recalll.co/app/?q=android%20webview%20-%20Split%20html%20string%20into%20multiple%20pages

How can I achieve page-based (non-vertical scrolling) application given the HTML content?

Community
  • 1
  • 1
karzler007
  • 111
  • 9

1 Answers1

1

I know this is an old question, but just in case:)

Your question has a very simple solution. Each object of UIWebView has a field called scrollView which is an object of UIScrollView class. UIScrollView support pagination by setting these values:

webView.scrollView.isPagingEnabled = true // enable pagination
webView.paginationMode = .leftToRight // set horizontal mode
webView.paginationBreakingMode = .page // pages or columns (latter need css support though)

Then you can fill your webView with data calling loadHTMLString:

guard let path = Bundle.main.path(forResource: "NameOfYourFile", ofType: "html") else {
    return
}
guard let data = FileManager.default.contents (atPath: path) else {
    return
}
guard let contentString = String(data: data, encoding: .utf8) else {
    return
}
let webView = UIWebView(frame: view.frame)
webView.loadHTMLString(contentString, baseURL: nil)    
view.addSubview(webView)

That's all you need to do here. For the record, I put the code lines above inside viewDidLoad method in my test project.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Solomiya
  • 310
  • 4
  • 15