Advertisement
Guest User

Untitled

a guest
Nov 15th, 2018
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 2.80 KB | None | 0 0
  1. //
  2. //  secondreaderViewController.swift
  3. //  Reader
  4. //
  5. //  Created by giovanni martusciello on 15/11/2018.
  6. //  Copyright © 2018 giovanni martusciello. All rights reserved.
  7. //
  8.  
  9. import UIKit
  10.  
  11. class secondreaderViewController: UIViewController, NSLayoutManagerDelegate {
  12.    
  13.     var scrollingView : UIScrollView?
  14.     let textStorage = NSTextStorage(string: textString)
  15.     let textLayout = NSLayoutManager()
  16.     var fontSize = 25
  17.  
  18.     @IBAction func biggerFont(_ sender: Any) {
  19.         fontSize += 5
  20.         print(fontSize)
  21.         setPages(fontSize: fontSize, textLayout: textLayout, scrollingView: scrollingView!)
  22.     }
  23.    
  24.     override func viewDidLoad() {
  25.        
  26.         super.viewDidLoad()
  27.        
  28.         scrollingView = UIScrollView(frame: CGRect(x: CGFloat(20), y: CGFloat(100), width: CGFloat(view.bounds.size.width - 40), height: CGFloat(view.bounds.size.height - 40)))
  29.        
  30.         textStorage.addLayoutManager(textLayout)
  31.         textLayout.delegate = self
  32.    
  33.         scrollingView!.isPagingEnabled = true
  34.         view.addSubview(scrollingView!)
  35.        
  36.         setPages(fontSize: fontSize, textLayout: textLayout, scrollingView: scrollingView!)
  37.     }
  38.    
  39.     func setPages(fontSize: Int, textLayout: NSLayoutManager, scrollingView: UIScrollView){
  40.        
  41.         textStorage.addLayoutManager(textLayout)
  42.         textLayout.delegate = self
  43.        
  44.         var r = CGRect(x: 0, y: 0, width: scrollingView.frame.size.width, height: scrollingView.frame.size.height)
  45.        
  46.         var i: Int = 0
  47.         print(i)
  48.         // this is what we'll use to track the "progress" of filling the "screens of textviews"
  49.         // each time through, we'll get the last Glyph rendered...
  50.         // if it's equal to the total number of Glyphs, we know we're done
  51.         var lastRenderedGlyph = 0
  52.        
  53.         while lastRenderedGlyph < textLayout.numberOfGlyphs {
  54.            
  55.             let textContainer = NSTextContainer(size: scrollingView.frame.size)
  56.             textLayout.addTextContainer(textContainer)
  57.            
  58.             let textView = UITextView(frame: r, textContainer: textContainer)
  59.            
  60.             r.origin.x += r.width
  61.            
  62.             textView.font = .systemFont(ofSize: CGFloat(fontSize))
  63.            
  64.             textView.tag = i
  65.            
  66.             i += 1
  67.            
  68.             scrollingView.addSubview(textView)
  69.            
  70.             // get the last Glyph rendered into the current textContainer
  71.             lastRenderedGlyph = NSMaxRange(textLayout.glyphRange(for: textContainer))
  72.            
  73.         }
  74.        
  75.         // use the last textView rect to set contentSize
  76.         scrollingView.contentSize = CGSize(width: r.origin.x, height: r.size.height)
  77.        
  78.         print("Actual number of pages =", i)
  79.     }
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement