Created
July 8, 2015 02:59
-
-
Save burhanaksendir/27292c84ca75385adede to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class BookViewController: UIViewController { | |
var book: UIView! | |
var content: NSMutableArray! | |
var currentPage: Int = 0 | |
var font: UIFont! | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
initWithText("Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium. Integer tincidunt. Cras dapibus. Vivamus elementum semper nisi. Aenean vulputate eleifend tellus. Aenean leo ligula, porttitor eu, consequat vitae, eleifend ac, enim. Aliquam lorem ante, dapibus in, viverra quis, feugiat a, tellus. Phasellus viverra nulla ut metus varius laoreet. Quisque rutrum. Aenean imperdiet. Etiam ultricies nisi vel augue. Curabitur ullamcorper ultricies nisi. Nam eget dui. Etiam rhoncus. Maecenas tempus, tellus eget condimentum rhoncus, sem quam semper libero, sit amet adipiscing sem neque sed ipsum. Nam quam nunc, blandit vel, luctus pulvinar, hendrerit id, lorem. Maecenas nec odio et ante tincidunt tempus. Donec vitae sapien ut libero venenatis faucibus. Nullam quis ante. Etiam sit amet orci eget eros faucibus tincidunt. Duis leo. Sed fringilla mauris sit amet nibh. Donec sodales sagittis magna. Sed consequat, leo eget bibendum sodales, augue velit cursus nunc, Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium. Integer tincidunt. Cras dapibus. Vivamus elementum semper nisi. Aenean vulputate eleifend tellus. Aenean leo ligula, porttitor eu, consequat vitae, eleifend ac, enim. Aliquam lorem ante, dapibus in, viverra quis, feugiat a, tellus. Phasellus viverra nulla ut metus varius laoreet. Quisque rutrum. Aenean imperdiet. Etiam ultricies nisi vel augue. Curabitur ullamcorper ultricies nisi. Nam eget dui. Etiam rhoncus. Maecenas tempus, tellus eget condimentum rhoncus, sem quam semper libero, sit amet adipiscing sem neque sed ipsum. Nam quam nunc, blandit vel, luctus pulvinar, hendrerit id, lorem. Maecenas nec odio et ante tincidunt tempus. Donec vitae sapien ut libero venenatis faucibus. Nullam quis ante. Etiam sit amet orci eget eros faucibus tincidunt. Duis leo. Sed fringilla mauris sit amet nibh. Donec sodales sagittis magna. Sed consequat, leo eget bibendum sodales, augue velit cursus nunc, Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium. Integer tincidunt. Cras dapibus. Vivamus elementum semper nisi. Aenean vulputate eleifend tellus. Aenean leo ligula, porttitor eu, consequat vitae, eleifend ac, enim. Aliquam lorem ante, dapibus in, viverra quis, feugiat a, tellus. Phasellus viverra nulla ut metus varius laoreet. Quisque rutrum. Aenean imperdiet. Etiam ultricies nisi vel augue. Curabitur ullamcorper ultricies nisi. Nam eget dui. Etiam rhoncus. Maecenas tempus, tellus eget condimentum rhoncus, sem quam semper libero, sit amet adipiscing sem neque sed ipsum. Nam quam nunc, blandit vel, luctus pulvinar, hendrerit id, lorem. Maecenas nec odio et ante tincidunt tempus. Donec vitae sapien ut libero venenatis faucibus. Nullam quis ante. Etiam sit amet orci eget eros faucibus tincidunt. Duis leo. Sed fringilla mauris sit amet nibh. Donec sodales sagittis magna. Sed consequat, leo eget bibendum sodales, augue velit cursus nunc, ") | |
loadViews() | |
} | |
func initWithText(text: NSString){ | |
font = UIFont(name: "Helvetica", size: 16) | |
content = NSMutableArray() | |
self.cutInPages(text) | |
currentPage = 0 | |
} | |
func sizeOfString (string: NSString, constrainedToWidth width: Double) -> CGSize { | |
let paragraphStyle = NSMutableParagraphStyle() | |
paragraphStyle.lineBreakMode = NSLineBreakMode.ByWordWrapping | |
return string.boundingRectWithSize(CGSize(width: width, height: DBL_MAX), | |
options: NSStringDrawingOptions.UsesFontLeading, | |
attributes: [NSFontAttributeName: font, NSParagraphStyleAttributeName: paragraphStyle], | |
context: nil).size | |
} | |
func cutInPages(text: NSString) { | |
var whereToCut: NSRange = text.rangeOfString(" ") | |
var pageText: NSString = text.substringToIndex(whereToCut.location) | |
var rest: NSString = text.substringFromIndex(whereToCut.location) | |
var height: CGFloat = 0 | |
while height < self.view.bounds.height { | |
whereToCut = rest.rangeOfString(" ") | |
var wordOfRest: NSString = rest.substringToIndex(whereToCut.location) | |
pageText = NSString(format: "%@%@", pageText, wordOfRest) | |
rest = rest.substringFromIndex(whereToCut.location) | |
height = sizeOfString(pageText, constrainedToWidth: Double(self.view.bounds.width)).height | |
} | |
if height > self.view.bounds.height { | |
//TODO cut the last word of pageText and prepend to the eest | |
} | |
content.addObject(pageText) | |
if rest.length > 0 { | |
self.cutInPages(rest) | |
} | |
} | |
func loadViews() { | |
book = UIView(frame: self.view.bounds) | |
var flipper: UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: Selector("nextOrPrevious:")) | |
book.addGestureRecognizer(flipper) | |
self.view.addSubview(book) | |
var textView: UITextView = UITextView() | |
initWithText(content.objectAtIndex(currentPage) as! NSString) | |
textView.frame = book.bounds | |
textView.font = font | |
textView.tag = 23 | |
book.addSubview(textView) | |
} | |
func nextOrPrevious(sender: UISwipeGestureRecognizer) { | |
var flipper: UISwipeGestureRecognizer = sender | |
if flipper.direction == UISwipeGestureRecognizerDirection.Left { | |
self.next() | |
}else if flipper.direction == UISwipeGestureRecognizerDirection.Right { | |
self.previous() | |
} | |
} | |
func next() { | |
if currentPage == content.count - 1 { | |
return | |
} | |
currentPage++ | |
var fromView: UIView = book.viewWithTag(23)! | |
var toView: UIView = UITextView() | |
initWithText(content.objectAtIndex(currentPage) as! NSString) | |
toView.frame = book.bounds | |
toView.tag = 23 | |
UIView.transitionWithView(book, duration: 0.2, options: UIViewAnimationOptions.TransitionFlipFromRight, animations: { () -> Void in | |
fromView.removeFromSuperview() | |
self.book.addSubview(toView) | |
}, completion: nil) | |
} | |
func previous() { | |
if currentPage == 0 { | |
return | |
} | |
currentPage-- | |
var fromView: UIView = book.viewWithTag(23)! | |
var toView: UIView = UITextView() | |
initWithText(content.objectAtIndex(currentPage) as! NSString) | |
toView.frame = book.bounds | |
toView.tag = 23 | |
UIView.transitionWithView(book, duration: 0.2, options: UIViewAnimationOptions.TransitionFlipFromRight, animations: { () -> Void in | |
fromView.removeFromSuperview() | |
self.book.addSubview(toView) | |
}, completion: nil) | |
} | |
override func didReceiveMemoryWarning() { | |
super.didReceiveMemoryWarning() | |
// Dispose of any resources that can be recreated. | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment