Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // secondreaderViewController.swift
- // Reader
- //
- // Created by giovanni martusciello on 15/11/2018.
- // Copyright © 2018 giovanni martusciello. All rights reserved.
- //
- import UIKit
- class secondreaderViewController: UIViewController, NSLayoutManagerDelegate {
- var scrollingView : UIScrollView?
- let textStorage = NSTextStorage(string: textString)
- let textLayout = NSLayoutManager()
- var fontSize = 25
- @IBAction func biggerFont(_ sender: Any) {
- fontSize += 5
- print(fontSize)
- setPages(fontSize: fontSize, textLayout: textLayout, scrollingView: scrollingView!)
- }
- override func viewDidLoad() {
- super.viewDidLoad()
- scrollingView = UIScrollView(frame: CGRect(x: CGFloat(20), y: CGFloat(100), width: CGFloat(view.bounds.size.width - 40), height: CGFloat(view.bounds.size.height - 40)))
- textStorage.addLayoutManager(textLayout)
- textLayout.delegate = self
- scrollingView!.isPagingEnabled = true
- view.addSubview(scrollingView!)
- setPages(fontSize: fontSize, textLayout: textLayout, scrollingView: scrollingView!)
- }
- func setPages(fontSize: Int, textLayout: NSLayoutManager, scrollingView: UIScrollView){
- textStorage.addLayoutManager(textLayout)
- textLayout.delegate = self
- var r = CGRect(x: 0, y: 0, width: scrollingView.frame.size.width, height: scrollingView.frame.size.height)
- var i: Int = 0
- print(i)
- // this is what we'll use to track the "progress" of filling the "screens of textviews"
- // each time through, we'll get the last Glyph rendered...
- // if it's equal to the total number of Glyphs, we know we're done
- var lastRenderedGlyph = 0
- while lastRenderedGlyph < textLayout.numberOfGlyphs {
- let textContainer = NSTextContainer(size: scrollingView.frame.size)
- textLayout.addTextContainer(textContainer)
- let textView = UITextView(frame: r, textContainer: textContainer)
- r.origin.x += r.width
- textView.font = .systemFont(ofSize: CGFloat(fontSize))
- textView.tag = i
- i += 1
- scrollingView.addSubview(textView)
- // get the last Glyph rendered into the current textContainer
- lastRenderedGlyph = NSMaxRange(textLayout.glyphRange(for: textContainer))
- }
- // use the last textView rect to set contentSize
- scrollingView.contentSize = CGSize(width: r.origin.x, height: r.size.height)
- print("Actual number of pages =", i)
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement