Skip to content
This repository has been archived by the owner on Nov 26, 2020. It is now read-only.

Expose font settings - closes #33 #138

Merged
merged 17 commits into from
Sep 16, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions Example/StoryboardExample/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="10117" systemVersion="15G31" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" initialViewController="BYZ-38-t0r">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="10117" systemVersion="15G1004" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" initialViewController="BYZ-38-t0r">
<dependencies>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="10085"/>
Expand All @@ -19,27 +19,35 @@
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<containerView opaque="NO" contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="76c-NV-rFk">
<rect key="frame" x="100" y="100" width="500" height="400"/>
<rect key="frame" x="50" y="0.0" width="500" height="400"/>
<connections>
<segue destination="ohM-0F-lQn" kind="embed" id="GwJ-h1-STP"/>
</connections>
</containerView>
</subviews>
<color key="backgroundColor" white="0.0" alpha="1" colorSpace="calibratedWhite"/>
<constraints>
<constraint firstItem="wfy-db-euE" firstAttribute="top" secondItem="76c-NV-rFk" secondAttribute="bottom" constant="100" id="8t2-cj-MBZ"/>
<constraint firstItem="76c-NV-rFk" firstAttribute="leading" secondItem="8bC-Xf-vdC" secondAttribute="leading" constant="100" id="NRZ-uj-OHl"/>
<constraint firstItem="76c-NV-rFk" firstAttribute="top" secondItem="y3c-jy-aDJ" secondAttribute="bottom" constant="80" id="0D6-Fj-fwv"/>
<constraint firstItem="76c-NV-rFk" firstAttribute="top" secondItem="y3c-jy-aDJ" secondAttribute="bottom" id="1FY-cA-fGb"/>
<constraint firstItem="wfy-db-euE" firstAttribute="top" secondItem="76c-NV-rFk" secondAttribute="bottom" constant="200" id="8t2-cj-MBZ"/>
<constraint firstItem="76c-NV-rFk" firstAttribute="top" secondItem="8bC-Xf-vdC" secondAttribute="top" id="N9R-fw-Ou2"/>
<constraint firstItem="76c-NV-rFk" firstAttribute="leading" secondItem="8bC-Xf-vdC" secondAttribute="leading" constant="50" id="NRZ-uj-OHl"/>
<constraint firstItem="76c-NV-rFk" firstAttribute="top" secondItem="8bC-Xf-vdC" secondAttribute="top" constant="100" id="Oo2-Kp-m9H"/>
<constraint firstItem="76c-NV-rFk" firstAttribute="top" secondItem="y3c-jy-aDJ" secondAttribute="bottom" constant="100" id="QPp-IK-WD4"/>
<constraint firstAttribute="trailing" secondItem="76c-NV-rFk" secondAttribute="trailing" id="ZS5-v1-RfB"/>
<constraint firstAttribute="trailing" secondItem="76c-NV-rFk" secondAttribute="trailing" constant="50" id="ZS5-v1-RfB"/>
<constraint firstItem="76c-NV-rFk" firstAttribute="leading" secondItem="8bC-Xf-vdC" secondAttribute="leadingMargin" constant="20" id="aFJ-E1-Z7h"/>
<constraint firstItem="wfy-db-euE" firstAttribute="top" secondItem="76c-NV-rFk" secondAttribute="bottom" constant="100" id="bTg-k8-90V"/>
<constraint firstItem="76c-NV-rFk" firstAttribute="top" secondItem="y3c-jy-aDJ" secondAttribute="bottom" id="hhc-EW-4bw"/>
<constraint firstAttribute="trailingMargin" secondItem="76c-NV-rFk" secondAttribute="trailing" constant="20" id="vz9-fJ-FdF"/>
</constraints>
<variation key="default">
<mask key="constraints">
<exclude reference="0D6-Fj-fwv"/>
<exclude reference="1FY-cA-fGb"/>
<exclude reference="Oo2-Kp-m9H"/>
<exclude reference="QPp-IK-WD4"/>
<exclude reference="aFJ-E1-Z7h"/>
<exclude reference="hhc-EW-4bw"/>
<exclude reference="vz9-fJ-FdF"/>
<exclude reference="bTg-k8-90V"/>
</mask>
Expand Down
11 changes: 10 additions & 1 deletion Example/StoryboardExample/ExampleFolioReaderContainer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,16 @@ class ExampleFolioReaderContainer: FolioReaderContainer {

let config = FolioReaderConfig()
config.scrollDirection = .horizontalWithVerticalContent

config.shouldHideNavigationOnTap = false

// Print the chapter ID if one was clicked
// A chapter in "The Silver Chair" looks like this "<section class="chapter" title="Chapter I" epub:type="chapter" id="id70364673704880">"
// To knwo if a user tapped on a chapter we can listen to events on the class "chapter" and receive the id value
let listener = ClassBasedOnClickListener(schemeName: "chaptertapped", querySelector: ".chapter", attributeName: "id", onClickAction: { (parameterContent: String?) in
print("chapter with id: " + (parameterContent ?? "-") + " clicked")
})
config.classBasedOnClickListeners.append(listener)

guard let bookPath = NSBundle.mainBundle().pathForResource("The Silver Chair", ofType: "epub") else { return }
setupConfig(config, epubPath: bookPath)
}
Expand Down
25 changes: 15 additions & 10 deletions Source/FolioReaderAudioPlayer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ public class FolioReaderAudioPlayer: NSObject {

func play() {
if book.hasAudio() {
guard let currentPage = FolioReader.sharedInstance.readerCenter.currentPage else { return }
guard let currentPage = FolioReader.sharedInstance.readerCenter?.currentPage else { return }
currentPage.webView.js("playAudio()")
} else {
readCurrentSentence()
Expand Down Expand Up @@ -212,7 +212,7 @@ public class FolioReaderAudioPlayer: NSObject {
func playPrevChapter() {
stopPlayerTimer()
// Wait for "currentPage" to update, then request to play audio
FolioReader.sharedInstance.readerCenter.changePageToPrevious {
FolioReader.sharedInstance.readerCenter?.changePageToPrevious {
if self.isPlaying() {
self.play()
} else {
Expand All @@ -224,7 +224,7 @@ public class FolioReaderAudioPlayer: NSObject {
func playNextChapter() {
stopPlayerTimer()
// Wait for "currentPage" to update, then request to play audio
FolioReader.sharedInstance.readerCenter.changePageToNext {
FolioReader.sharedInstance.readerCenter?.changePageToNext {
if self.isPlaying() {
self.play()
}
Expand Down Expand Up @@ -293,7 +293,7 @@ public class FolioReaderAudioPlayer: NSObject {
// get the fragment ID so we can "mark" it in the webview
let textParts = textFragment!.componentsSeparatedByString("#")
let fragmentID = textParts[1];
FolioReader.sharedInstance.readerCenter.audioMark(href: currentHref, fragmentID: fragmentID)
FolioReader.sharedInstance.readerCenter?.audioMark(href: currentHref, fragmentID: fragmentID)

return true
}
Expand Down Expand Up @@ -353,18 +353,23 @@ public class FolioReaderAudioPlayer: NSObject {
// MARK: TTS Sentence

func speakSentence() {
guard let currentPage = FolioReader.sharedInstance.readerCenter.currentPage else { return }
guard let
readerCenter = FolioReader.sharedInstance.readerCenter,
currentPage = readerCenter.currentPage else {
return
}

let sentence = currentPage.webView.js("getSentenceWithIndex('\(book.playbackActiveClass())')")

if sentence != nil {
let chapter = FolioReader.sharedInstance.readerCenter.getCurrentChapter()
let chapter = readerCenter.getCurrentChapter()
let href = chapter != nil ? chapter!.href : "";
playText(href, text: sentence!)
} else {
if FolioReader.sharedInstance.readerCenter.isLastPage() {
if readerCenter.isLastPage() {
stop()
} else {
FolioReader.sharedInstance.readerCenter.changePageToNext()
readerCenter.changePageToNext()
}
}
}
Expand All @@ -378,7 +383,7 @@ public class FolioReaderAudioPlayer: NSObject {
} else {
if synthesizer.speaking {
stopSynthesizer(immediate: false, completion: {
if let currentPage = FolioReader.sharedInstance.readerCenter.currentPage {
if let currentPage = FolioReader.sharedInstance.readerCenter?.currentPage {
currentPage.webView.js("resetCurrentSentenceIndex()")
}
self.speakSentence()
Expand Down Expand Up @@ -463,7 +468,7 @@ public class FolioReaderAudioPlayer: NSObject {
the `currentPage` in ReaderCenter may not have updated just yet
*/
func getCurrentChapterName() -> String? {
guard let chapter = FolioReader.sharedInstance.readerCenter.getCurrentChapter() else {
guard let chapter = FolioReader.sharedInstance.readerCenter?.getCurrentChapter() else {
return nil
}

Expand Down
41 changes: 2 additions & 39 deletions Source/FolioReaderCenter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -358,25 +358,7 @@ public class FolioReaderCenter: UIViewController, UICollectionViewDelegate, UICo
html = html?.stringByReplacingOccurrencesOfString("</head>", withString: toInject)

// Font class name
var classes = ""
let currentFontName = FolioReader.currentFontName
switch currentFontName {
case 0:
classes = "andada"
break
case 1:
classes = "lato"
break
case 2:
classes = "lora"
break
case 3:
classes = "raleway"
break
default:
break
}

var classes = FolioReader.currentFont.cssIdentifier
classes += " "+FolioReader.currentMediaOverlayStyle.className()

// Night mode
Expand All @@ -385,26 +367,7 @@ public class FolioReaderCenter: UIViewController, UICollectionViewDelegate, UICo
}

// Font Size
let currentFontSize = FolioReader.currentFontSize
switch currentFontSize {
case 0:
classes += " textSizeOne"
break
case 1:
classes += " textSizeTwo"
break
case 2:
classes += " textSizeThree"
break
case 3:
classes += " textSizeFour"
break
case 4:
classes += " textSizeFive"
break
default:
break
}
classes += " \(FolioReader.currentFontSize.cssIdentifier)"

html = html?.stringByReplacingOccurrencesOfString("<html ", withString: "<html class=\"\(classes)\"")

Expand Down
50 changes: 49 additions & 1 deletion Source/FolioReaderConfig.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

import UIKit

// MARK: - FolioReaderScrollDirection

/**
Defines the Reader scrolling direction
*/
Expand Down Expand Up @@ -37,12 +39,58 @@ public enum FolioReaderScrollDirection: Int {
}
}

// MARK: - ClassBasedOnClickListener

/**
A `ClassBasedOnClickListener` takes a closure which is performed if a given html `class` is clicked. The closure will reveice the content of the specified parameter.

Eg. A ClassBasedOnClickListener with the className "quote" and parameterName "id" with the given epub html content "<section class="quote" id="12345">" would call the given closure on a click on this section with the String "12345" as parameter.

*/
public struct ClassBasedOnClickListener {

/// The name of the URL scheme which should be used. Note: Make sure that the given `String` is a valid as scheme name.
public var schemeName : String

/// The query selector for the elements which the listener should be added to. See https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelector for further information about query selectors.
public var querySelector : String

/// The name of the attribute whose content should be passed to the `onClickAction` action.
public var attributeName : String

/// Whether the listener should be added to all found elements or only to the first one. See https://developer.mozilla.org/en-US/docs/Web/API/Element/querySelectorAll for further information. The default value is `true`.
public var selectAll : Bool

/// The closure which will be called if the specified class was clicked.
public var onClickAction : ((parameterContent: String?) -> Void)

/// Initializes a `ClassBasedOnClickListener` instance. Append it to the `classBasedOnClickListeners` property from the `FolioReaderConfig` to receive on click events. The default `selectAll` value is `true`.
public init(schemeName: String, querySelector: String, attributeName: String, selectAll: Bool = true, onClickAction: ((attributeContent: String?) -> Void)) {
self.schemeName = schemeName.lowercaseString
self.querySelector = querySelector
self.attributeName = attributeName
self.selectAll = selectAll
self.onClickAction = onClickAction
}
}

// MARK: - FolioReaderConfig

/**
Defines the Reader custom configuration
*/
public class FolioReaderConfig: NSObject {


// MARK: ClassBasedOnClickListener

/**
Array of `ClassBasedOnClickListener` objects. A `ClassBasedOnClickListener` takes a closure which is performed if a given html `class` is clicked. The closure will reveice the content of the specified parameter.

Eg. A ClassBasedOnClickListener with the className "quote" and parameterName "id" with the given epub html content "<section class="quote" id="12345">" would call the given closure on a click on this section with the String "12345" as parameter.

*/
public var classBasedOnClickListeners = [ClassBasedOnClickListener]()

// MARK: Colors

/// Base header custom TintColor
Expand Down
4 changes: 2 additions & 2 deletions Source/FolioReaderContainer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,10 @@ public class FolioReaderContainer: UIViewController {

// Register custom fonts
FontBlaster.blast(NSBundle.frameworkBundle())

// Register initial defaults
FolioReader.defaults.registerDefaults([
kCurrentFontFamily: 0,
kCurrentFontFamily: FolioReaderFont.Andada.rawValue,
kNightMode: false,
kCurrentFontSize: 2,
kCurrentAudioRate: 1,
Expand Down
Loading