Commit ab349a90 by Daniel Dahan

editor: added EditorController class

parent 73bfa32f
......@@ -8,6 +8,7 @@
/* Begin PBXBuildFile section */
961DED461DCC40C500F425B6 /* Editor.swift in Sources */ = {isa = PBXBuildFile; fileRef = 961DED451DCC40C500F425B6 /* Editor.swift */; };
961DED4B1DCC546100F425B6 /* EditorController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 961DED4A1DCC546100F425B6 /* EditorController.swift */; };
961EFC581D738FF600E84652 /* SnackbarController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 961EFC571D738FF600E84652 /* SnackbarController.swift */; };
961F18E81CD93E3E008927C5 /* ErrorTextField.swift in Sources */ = {isa = PBXBuildFile; fileRef = 961F18E71CD93E3E008927C5 /* ErrorTextField.swift */; };
96230AB81D6A520C00AF47DC /* Divider.swift in Sources */ = {isa = PBXBuildFile; fileRef = 96230AB71D6A520C00AF47DC /* Divider.swift */; };
......@@ -205,6 +206,7 @@
/* Begin PBXFileReference section */
961DED451DCC40C500F425B6 /* Editor.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Editor.swift; sourceTree = "<group>"; };
961DED4A1DCC546100F425B6 /* EditorController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = EditorController.swift; sourceTree = "<group>"; };
961EFC571D738FF600E84652 /* SnackbarController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SnackbarController.swift; sourceTree = "<group>"; };
961F18E71CD93E3E008927C5 /* ErrorTextField.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ErrorTextField.swift; sourceTree = "<group>"; };
96230AB71D6A520C00AF47DC /* Divider.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Divider.swift; sourceTree = "<group>"; };
......@@ -361,6 +363,7 @@
isa = PBXGroup;
children = (
961DED451DCC40C500F425B6 /* Editor.swift */,
961DED4A1DCC546100F425B6 /* EditorController.swift */,
);
name = Editor;
sourceTree = "<group>";
......@@ -1075,6 +1078,7 @@
96BCB7A91CB40DC500C806FE /* FlatButton.swift in Sources */,
96BCB7CA1CB40DC500C806FE /* Layout.swift in Sources */,
96BCB7D11CB40DC500C806FE /* Switch.swift in Sources */,
961DED4B1DCC546100F425B6 /* EditorController.swift in Sources */,
96BCB7BA1CB40DC500C806FE /* CollectionView.swift in Sources */,
96BCB7BC1CB40DC500C806FE /* CollectionViewDataSource.swift in Sources */,
96717B191DBE6B1800DA84DB /* PhotoLibraryController.swift in Sources */,
......
......@@ -49,7 +49,7 @@ extension UIViewController {
}
}
open class CaptureController: ToolbarController, CaptureDelegate {
open class CaptureController: ToolbarController {
/// A reference to the Capture instance.
@IBInspectable
open private(set) lazy var capture: Capture = Capture()
......@@ -93,7 +93,8 @@ open class CaptureController: ToolbarController, CaptureDelegate {
/// Prepares capture.
private func prepareCapture() {
capture.delegate = self
capture.delegate = self
capture.flashMode = .auto
}
}
extension CaptureController: CaptureDelegate {}
......@@ -30,78 +30,111 @@
import UIKit
public enum CharacterAttributePreset {
case font
case paragraphStyle
case forgroundColor
case backgroundColor
case ligature
case kern
case strikethroughStyle
case underlineStyle
case strokeColor
case strokeWidth
case shadow
case textEffect
case attachment
case baselineOffset
case underlineColor
case strikethroughColor
case obliqueness
case expansion
case writingDirection
case verticalGlyphForm
public enum CharacterAttribute: String {
case font = "NSFontAttributeName"
case paragraphStyle = "NSParagraphStyleAttributeName"
case forgroundColor = "NSForegroundColorAttributeName"
case backgroundColor = "NSBackgroundColorAttributeName"
case ligature = "NSLigatureAttributeName"
case kern = "NSKernAttributeName"
case strikethroughStyle = "NSStrikethroughStyleAttributeName"
case underlineStyle = "NSUnderlineStyleAttributeName"
case strokeColor = "NSStrokeColorAttributeName"
case strokeWidth = "NSStrokeWidthAttributeName"
case shadow = "NSShadowAttributeName"
case textEffect = "NSTextEffectAttributeName"
case attachment = "NSAttachmentAttributeName"
case link = "NSLinkAttributeName"
case baselineOffset = "NSBaselineOffsetAttributeName"
case underlineColor = "NSUnderlineColorAttributeName"
case strikethroughColor = "NSStrikethroughColorAttributeName"
case obliqueness = "NSObliquenessAttributeName"
case expansion = "NSExpansionAttributeName"
case writingDirection = "NSWritingDirectionAttributeName"
case verticalGlyphForm = "NSVerticalGlyphFormAttributeName"
}
public func CharacterSpacePresetToValue(preset: CharacterAttributePreset) -> String {
// Predefined character attributes for text. If the key is not in the dictionary, then use the default values as described below.
/************************ Attributes ************************/
@available(iOS 6.0, *)
public let NSFontAttributeName: String // UIFont, default Helvetica(Neue) 12
@available(iOS 6.0, *)
public let NSParagraphStyleAttributeName: String // NSParagraphStyle, default defaultParagraphStyle
@available(iOS 6.0, *)
public let NSForegroundColorAttributeName: String // UIColor, default blackColor
@available(iOS 6.0, *)
public let NSBackgroundColorAttributeName: String // UIColor, default nil: no background
@available(iOS 6.0, *)
public let NSLigatureAttributeName: String // NSNumber containing integer, default 1: default ligatures, 0: no ligatures
@available(iOS 6.0, *)
public let NSKernAttributeName: String // NSNumber containing floating point value, in points; amount to modify default kerning. 0 means kerning is disabled.
@available(iOS 6.0, *)
public let NSStrikethroughStyleAttributeName: String // NSNumber containing integer, default 0: no strikethrough
@available(iOS 6.0, *)
public let NSUnderlineStyleAttributeName: String // NSNumber containing integer, default 0: no underline
@available(iOS 6.0, *)
public let NSStrokeColorAttributeName: String // UIColor, default nil: same as foreground color
@available(iOS 6.0, *)
public let NSStrokeWidthAttributeName: String // NSNumber containing floating point value, in percent of font point size, default 0: no stroke; positive for stroke alone, negative for stroke and fill (a typical value for outlined text would be 3.0)
@available(iOS 6.0, *)
public let NSShadowAttributeName: String // NSShadow, default nil: no shadow
@available(iOS 7.0, *)
public let NSTextEffectAttributeName: String // NSString, default nil: no text effect
@available(iOS 7.0, *)
public let NSAttachmentAttributeName: String // NSTextAttachment, default nil
@available(iOS 7.0, *)
public let NSLinkAttributeName: String // NSURL (preferred) or NSString
@available(iOS 7.0, *)
public let NSBaselineOffsetAttributeName: String // NSNumber containing floating point value, in points; offset from baseline, default 0
@available(iOS 7.0, *)
public let NSUnderlineColorAttributeName: String // UIColor, default nil: same as foreground color
@available(iOS 7.0, *)
public let NSStrikethroughColorAttributeName: String // UIColor, default nil: same as foreground color
@available(iOS 7.0, *)
public let NSObliquenessAttributeName: String // NSNumber containing floating point value; skew to be applied to glyphs, default 0: no skew
@available(iOS 7.0, *)
public let NSExpansionAttributeName: String // NSNumber containing floating point value; log of expansion factor to be applied to glyphs, default 0: no expansion
@available(iOS 7.0, *)
public let NSWritingDirectionAttributeName: String // NSArray of NSNumbers representing the nested levels of writing direction overrides as defined by Unicode LRE, RLE, LRO, and RLO characters. The control characters can be obtained by masking NSWritingDirection and NSWritingDirectionFormatType values. LRE: NSWritingDirectionLeftToRight|NSWritingDirectionEmbedding, RLE: NSWritingDirectionRightToLeft|NSWritingDirectionEmbedding, LRO: NSWritingDirectionLeftToRight|NSWritingDirectionOverride, RLO: NSWritingDirectionRightToLeft|NSWritingDirectionOverride,
@available(iOS 6.0, *)
public let NSVerticalGlyphFormAttributeName: String // An NSNumber containing an integer value. 0 means horizontal text. 1 indicates vertical text. If not specified, it could follow higher-level vertical orientation settings. Currently on iOS, it's always horizontal. The behavior for any other value is undefined.
public func CharacterAttributeToValue(preset: CharacterAttribute) -> String {
switch preset {
case .font:
return NSFontAttributeName
case .paragraphStyle:
return NSParagraphStyleAttributeName
case .forgroundColor:
return NSForegroundColorAttributeName
case .backgroundColor:
return NSBackgroundColorAttributeName
case .ligature:
return NSLigatureAttributeName
case .kern:
return NSKernAttributeName
case .strikethroughStyle:
return NSStrikethroughStyleAttributeName
case .underlineStyle:
return NSUnderlineStyleAttributeName
case .strokeColor:
return NSStrokeColorAttributeName
case .strokeWidth:
return NSStrokeWidthAttributeName
case .shadow:
return NSShadowAttributeName
case .textEffect:
return NSTextEffectAttributeName
case .attachment:
return NSAttachmentAttributeName
case .link:
return NSLinkAttributeName
case .baselineOffset:
return NSBaselineOffsetAttributeName
case .underlineColor:
return NSUnderlineColorAttributeName
case .strikethroughColor:
return NSStrikethroughColorAttributeName
case .obliqueness:
return NSObliquenessAttributeName
case .expansion:
return NSExpansionAttributeName
case .writingDirection:
return NSWritingDirectionAttributeName
case .verticalGlyphForm:
return NSVerticalGlyphFormAttributeName
}
}
/// A memory reference to the CharacterAttribute instance for String extensions.
//private var CharacterAttributeKey: UInt8 = 0
//public struct CharacterAttribute {
//}
//extension String {
/// characterAttribute reference.
// public private(set) var characterAttribute: CharacterAttribute {
// get {
// return AssociatedObject(base: self, key: &CharacterAttributeKey) {
// return CharacterAttribute()
// }
// }
// set(value) {
// AssociateObject(base: self, key: &CharacterAttributeKey, value: value)
// }
// }
//}
extension NSAttributedString {
open func characterAttributes(at index: Int, effectiveRange: NSRangePointer?) -> [CharacterAttribute: Any] {
var ca = [CharacterAttribute: Any]()
attributes(at: index, effectiveRange: effectiveRange).forEach { (key, value) in
guard let attribute = CharacterAttribute.init(rawValue: key) else {
return
}
ca[attribute] = value
}
return ca
}
}
public enum ParagraphAttribute {
......@@ -111,3 +144,14 @@ public enum ParagraphAttribute {
public enum DocumentAttribute {
}
@objc(EditorDelegate)
public protocol EditorDelegate {
}
open class Editor: View {
/// A reference to an EditorDelegate.
open weak var delegate: EditorDelegate?
}
/*
* Copyright (C) 2015 - 2016, Daniel Dahan and CosmicMind, Inc. <http://cosmicmind.com>.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* * Neither the name of CosmicMind nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
import UIKit
import AVFoundation
extension UIViewController {
/**
A convenience property that provides access to the EditorController.
This is the recommended method of accessing the EditorController
through child UIViewControllers.
*/
public var editorController: EditorController? {
var viewController: UIViewController? = self
while nil != viewController {
if viewController is EditorController {
return viewController as? EditorController
}
viewController = viewController?.parent
}
return nil
}
}
open class EditorController: ToolbarController {
/// A reference to the Editor instance.
@IBInspectable
open private(set) lazy var editor: Editor = Editor()
/**
Prepares the view instance when intialized. When subclassing,
it is recommended to override the prepare method
to initialize property values and other setup operations.
The super.prepare method should always be called immediately
when subclassing.
*/
open override func prepare() {
super.prepare()
display = .full
view.backgroundColor = .black
prepareStatusBar()
prepareToolbar()
prepareEditor()
}
/// Prepares the statusBar.
private func prepareStatusBar() {
statusBar.backgroundColor = .clear
}
/// Prepares the toolbar.
private func prepareToolbar() {
toolbar.backgroundColor = .clear
toolbar.depthPreset = .none
}
/// Prepares editor.
private func prepareEditor() {
editor.delegate = self
}
}
extension EditorController: EditorDelegate {}
......@@ -30,6 +30,24 @@
import UIKit
extension UIViewController {
/**
A convenience property that provides access to the PhotoLibraryController.
This is the recommended method of accessing the PhotoLibraryController
through child UIViewControllers.
*/
public var photoLibraryController: PhotoLibraryController? {
var viewController: UIViewController? = self
while nil != viewController {
if viewController is PhotoLibraryController {
return viewController as? PhotoLibraryController
}
viewController = viewController?.parent
}
return nil
}
}
open class PhotoLibraryController: UIViewController {
/// A reference to a PhotoLibrary.
open private(set) lazy var photoLibrary: PhotoLibrary = PhotoLibrary()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment