Commit d1f9360b by Daniel Dahan

development: cleaning up Editor

parent fa08e443
......@@ -19,7 +19,6 @@
9617B0821DFCA8CF00410F8F /* CapturePreview.swift in Headers */ = {isa = PBXBuildFile; fileRef = 96717B0F1DBE6AF600DA84DB /* CapturePreview.swift */; settings = {ATTRIBUTES = (Public, ); }; };
9617B0831DFCA8CF00410F8F /* CaptureController.swift in Headers */ = {isa = PBXBuildFile; fileRef = 96717B0E1DBE6AF600DA84DB /* CaptureController.swift */; settings = {ATTRIBUTES = (Public, ); }; };
9617B0841DFCA8CF00410F8F /* Editor.swift in Headers */ = {isa = PBXBuildFile; fileRef = 961DED451DCC40C500F425B6 /* Editor.swift */; settings = {ATTRIBUTES = (Public, ); }; };
9617B0851DFCA8CF00410F8F /* EditorController.swift in Headers */ = {isa = PBXBuildFile; fileRef = 961DED4A1DCC546100F425B6 /* EditorController.swift */; settings = {ATTRIBUTES = (Public, ); }; };
9617B0861DFCA8CF00410F8F /* HeightPreset.swift in Headers */ = {isa = PBXBuildFile; fileRef = 9626CB9A1DAD3D1D003E2611 /* HeightPreset.swift */; settings = {ATTRIBUTES = (Public, ); }; };
9617B0871DFCA8CF00410F8F /* PageTabBarController.swift in Headers */ = {isa = PBXBuildFile; fileRef = 963FBF071D669D14008F8512 /* PageTabBarController.swift */; settings = {ATTRIBUTES = (Public, ); }; };
9617B0881DFCA8CF00410F8F /* PhotoLibrary.swift in Headers */ = {isa = PBXBuildFile; fileRef = 96717B161DBE6B1800DA84DB /* PhotoLibrary.swift */; settings = {ATTRIBUTES = (Public, ); }; };
......@@ -118,7 +117,6 @@
965E81221DD4D5C800D61E4B /* TextView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 96BCB79E1CB40DC500C806FE /* TextView.swift */; };
965E81231DD4D7C800D61E4B /* BottomTabBar.swift in Sources */ = {isa = PBXBuildFile; fileRef = 96BCB7591CB40DC500C806FE /* BottomTabBar.swift */; };
965E81241DD4D7C800D61E4B /* Editor.swift in Sources */ = {isa = PBXBuildFile; fileRef = 961DED451DCC40C500F425B6 /* Editor.swift */; };
965E81251DD4D7C800D61E4B /* EditorController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 961DED4A1DCC546100F425B6 /* EditorController.swift */; };
965E81261DD4D7C800D61E4B /* CharacterAttribute.swift in Sources */ = {isa = PBXBuildFile; fileRef = 961276621DCD8B1800A7D920 /* CharacterAttribute.swift */; };
9697F7BF1D8F2572004741EC /* Divider.swift in Headers */ = {isa = PBXBuildFile; fileRef = 96230AB71D6A520C00AF47DC /* Divider.swift */; settings = {ATTRIBUTES = (Public, ); }; };
9697F7C01D8F2572004741EC /* Material+CALayer.swift in Headers */ = {isa = PBXBuildFile; fileRef = 96F1DC871D654FDF0025F925 /* Material+CALayer.swift */; settings = {ATTRIBUTES = (Public, ); }; };
......@@ -196,7 +194,6 @@
961276621DCD8B1800A7D920 /* CharacterAttribute.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CharacterAttribute.swift; sourceTree = "<group>"; };
961730591E145DE900A9A297 /* CollectionViewCard.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CollectionViewCard.swift; sourceTree = "<group>"; };
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>"; };
961E6BDE1DDA2A95004E6C93 /* Application.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Application.swift; sourceTree = "<group>"; };
961E6BE11DDA2AF3004E6C93 /* Screen.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Screen.swift; sourceTree = "<group>"; };
961EFC571D738FF600E84652 /* SnackbarController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SnackbarController.swift; sourceTree = "<group>"; };
......@@ -315,7 +312,6 @@
isa = PBXGroup;
children = (
961DED451DCC40C500F425B6 /* Editor.swift */,
961DED4A1DCC546100F425B6 /* EditorController.swift */,
);
name = Editor;
sourceTree = "<group>";
......@@ -822,7 +818,6 @@
9617B0821DFCA8CF00410F8F /* CapturePreview.swift in Headers */,
9617B0831DFCA8CF00410F8F /* CaptureController.swift in Headers */,
9617B0841DFCA8CF00410F8F /* Editor.swift in Headers */,
9617B0851DFCA8CF00410F8F /* EditorController.swift in Headers */,
9617B0861DFCA8CF00410F8F /* HeightPreset.swift in Headers */,
9617B0871DFCA8CF00410F8F /* PageTabBarController.swift in Headers */,
9617B0881DFCA8CF00410F8F /* PhotoLibrary.swift in Headers */,
......@@ -929,7 +924,6 @@
files = (
965E81231DD4D7C800D61E4B /* BottomTabBar.swift in Sources */,
965E81241DD4D7C800D61E4B /* Editor.swift in Sources */,
965E81251DD4D7C800D61E4B /* EditorController.swift in Sources */,
961E6BE21DDA2AF3004E6C93 /* Screen.swift in Sources */,
965E81261DD4D7C800D61E4B /* CharacterAttribute.swift in Sources */,
965E80FF1DD4D5C800D61E4B /* BottomNavigationController.swift in Sources */,
......
......@@ -324,8 +324,9 @@ extension Editor {
/// Prepares the keyboard notification center observers.
fileprivate func prepareKeyboardNotificationObservers() {
NotificationCenter.default.addObserver(self, selector: #selector(handleKeyboardWillShow(notification:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(handleKeyboardWillHide(notification:)), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
let defaultCenter = NotificationCenter.default
defaultCenter.addObserver(self, selector: #selector(handleKeyboardWillShow(notification:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
defaultCenter.addObserver(self, selector: #selector(handleKeyboardWillHide(notification:)), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
}
}
......
/*
* Copyright (C) 2015 - 2017, 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 let 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()
view.backgroundColor = .white
prepareToolbar()
prepareEditor()
}
/// Prepares the toolbar.
private func prepareToolbar() {
toolbar.depthPreset = .none
}
/// Prepares editor.
private func prepareEditor() {
editor.delegate = self
}
}
extension EditorController: EditorDelegate {}
......@@ -35,6 +35,14 @@ public protocol TextViewDelegate : UITextViewDelegate {}
@objc(TextView)
open class TextView: UITextView {
/// A boolean indicating whether the text is empty.
open var isEmpty: Bool {
return 0 == text?.utf16.count
}
/// A boolean indicating whether the text is in edit mode.
open fileprivate(set) var isEditing = true
/// A property that accesses the backing layer's background
@IBInspectable
open override var backgroundColor: UIColor? {
......@@ -43,56 +51,49 @@ open class TextView: UITextView {
}
}
/**
The title UILabel that is displayed when there is text. The
titleLabel text value is updated with the placeholderLabel
text value before being displayed.
*/
@IBInspectable
open var titleLabel: UILabel? {
didSet {
prepareTitleLabel()
}
}
/// The color of the titleLabel text when the textView is not active.
@IBInspectable
open var titleLabelColor: UIColor? {
didSet {
titleLabel?.textColor = titleLabelColor
}
}
/// The color of the titleLabel text when the textView is active.
@IBInspectable
open var titleLabelActiveColor: UIColor?
/**
A property that sets the distance between the textView and
titleLabel.
*/
@IBInspectable
open var titleLabelAnimationDistance: CGFloat = 8
/// Placeholder UILabel view.
open var placeholderLabel: UILabel? {
didSet {
preparePlaceholderLabel()
}
}
/// The placeholderLabel font value.
@IBInspectable
open override var font: UIFont? {
didSet {
placeholderLabel.font = font
}
}
/// The placeholderLabel text value.
@IBInspectable
open var placeholder: String? {
get {
return placeholderLabel?.text
return placeholderLabel.text
}
set(value) {
placeholderLabel?.text = value
placeholderLabel.text = value
layoutSubviews()
}
}
/// The placeholder UILabel.
@IBInspectable
open let placeholderLabel = UILabel()
/// Placeholder normal text
@IBInspectable
open var placeholderNormalColor = Color.darkText.others {
didSet {
updatePlaceholderLabelColor()
}
}
/// Placeholder active text
@IBInspectable
open var placeholderActiveColor = Color.blue.base {
didSet {
updatePlaceholderLabelColor()
}
}
/// An override to the text property.
@IBInspectable
open override var text: String! {
open override var text: String? {
didSet {
handleTextViewTextDidChange()
}
......@@ -106,9 +107,9 @@ open class TextView: UITextView {
}
/**
Text container UIEdgeInset preset property. This updates the
textContainerInset property with a preset value.
*/
Text container UIEdgeInset preset property. This updates the
textContainerInset property with a preset value.
*/
open var textContainerEdgeInsetsPreset: EdgeInsetsPreset = .none {
didSet {
textContainerInset = EdgeInsetsPresetToValue(preset: textContainerEdgeInsetsPreset)
......@@ -123,39 +124,40 @@ open class TextView: UITextView {
}
/**
An initializer that initializes the object with a NSCoder object.
- Parameter aDecoder: A NSCoder instance.
*/
An initializer that initializes the object with a NSCoder object.
- Parameter aDecoder: A NSCoder instance.
*/
public required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
prepare()
}
/**
An initializer that initializes the object with a CGRect object.
If AutoLayout is used, it is better to initilize the instance
using the init() initializer.
- Parameter frame: A CGRect instance.
- Parameter textContainer: A NSTextContainer instance.
*/
An initializer that initializes the object with a CGRect object.
If AutoLayout is used, it is better to initilize the instance
using the init() initializer.
- Parameter frame: A CGRect instance.
- Parameter textContainer: A NSTextContainer instance.
*/
public override init(frame: CGRect, textContainer: NSTextContainer?) {
super.init(frame: frame, textContainer: textContainer)
prepare()
}
/**
A convenience initializer that is mostly used with AutoLayout.
- Parameter textContainer: A NSTextContainer instance.
*/
A convenience initializer that is mostly used with AutoLayout.
- Parameter textContainer: A NSTextContainer instance.
*/
public convenience init(textContainer: NSTextContainer?) {
self.init(frame: .zero, textContainer: textContainer)
}
/** Denitializer. This should never be called unless you know
what you are doing.
*/
/**
Denitializer. This should never be called unless you know
what you are doing.
*/
deinit {
removeNotificationHandlers()
NotificationCenter.default.removeObserver(self)
}
open override func layoutSubviews() {
......@@ -163,63 +165,36 @@ open class TextView: UITextView {
layoutShape()
layoutShadowPath()
placeholderLabel?.preferredMaxLayoutWidth = textContainer.size.width - textContainer.lineFragmentPadding * 2
titleLabel?.frame.size.width = bounds.width
placeholderLabel.preferredMaxLayoutWidth = textContainer.size.width - textContainer.lineFragmentPadding * 2
}
/// Reloads necessary components when the view has changed.
open func reload() {
if let p = placeholderLabel {
removeConstraints(constraints)
layout(p).edges(
top: textContainerInset.top,
left: textContainerInset.left + textContainer.lineFragmentPadding,
bottom: textContainerInset.bottom,
right: textContainerInset.right + textContainer.lineFragmentPadding)
}
removeConstraints(constraints)
layout(placeholderLabel).edges(
top: textContainerInset.top,
left: textContainerInset.left + textContainer.lineFragmentPadding,
bottom: textContainerInset.bottom,
right: textContainerInset.right + textContainer.lineFragmentPadding)
}
/// Notification handler for when text editing began.
@objc
fileprivate func handleTextViewTextDidBegin() {
titleLabel?.textColor = titleLabelActiveColor
}
}
/// Notification handler for when text changed.
@objc
fileprivate func handleTextViewTextDidChange() {
if let p = placeholderLabel {
p.isHidden = !(true == text?.isEmpty)
}
guard let t = text else {
hideTitleLabel()
return
}
if 0 < t.utf16.count {
showTitleLabel()
} else {
hideTitleLabel()
}
placeholderLabel.isHidden = !isEmpty
}
/// Notification handler for when text editing ended.
@objc
fileprivate func handleTextViewTextDidEnd() {
guard let t = text else {
hideTitleLabel()
return
}
if 0 < t.utf16.count {
showTitleLabel()
} else {
hideTitleLabel()
}
titleLabel?.textColor = titleLabelColor
}
}
/**
Prepares the view instance when intialized. When subclassing,
......@@ -232,72 +207,17 @@ open class TextView: UITextView {
contentScaleFactor = Screen.scale
textContainerInset = .zero
backgroundColor = .white
clipsToBounds = false
removeNotificationHandlers()
clipsToBounds = false
preparePlaceholderLabel()
prepareNotificationHandlers()
reload()
}
/// prepares the placeholderLabel property.
fileprivate func preparePlaceholderLabel() {
if let v: UILabel = placeholderLabel {
v.font = font
v.textAlignment = textAlignment
v.numberOfLines = 0
v.backgroundColor = .clear
addSubview(v)
reload()
handleTextViewTextDidChange()
}
}
/// Prepares the titleLabel property.
fileprivate func prepareTitleLabel() {
if let v: UILabel = titleLabel {
v.isHidden = true
addSubview(v)
guard let t = text, 0 == t.utf16.count else {
v.alpha = 0
return
}
showTitleLabel()
}
}
/// Shows and animates the titleLabel property.
fileprivate func showTitleLabel() {
if let v: UILabel = titleLabel {
if v.isHidden {
if let s: String = placeholderLabel?.text {
v.text = s
}
let h: CGFloat = ceil(v.font.lineHeight)
v.frame = CGRect(x: 0, y: -h, width: bounds.width, height: h)
v.isHidden = false
UIView.animate(withDuration: 0.25, animations: { [weak self] in
if let s: TextView = self {
v.alpha = 1
v.frame.origin.y = -v.frame.height - s.titleLabelAnimationDistance
}
})
}
}
}
/// Hides and animates the titleLabel property.
fileprivate func hideTitleLabel() {
if let v: UILabel = titleLabel {
if !v.isHidden {
UIView.animate(withDuration: 0.25, animations: {
v.alpha = 0
v.frame.origin.y = -v.frame.height
}) { _ in
v.isHidden = true
}
}
}
placeholderLabel.font = font
placeholderLabel.textAlignment = textAlignment
placeholderLabel.numberOfLines = 0
placeholderLabel.backgroundColor = .clear
}
/// Prepares the Notification handlers.
......@@ -307,12 +227,12 @@ open class TextView: UITextView {
defaultCenter.addObserver(self, selector: #selector(handleTextViewTextDidChange), name: NSNotification.Name.UITextViewTextDidChange, object: self)
defaultCenter.addObserver(self, selector: #selector(handleTextViewTextDidEnd), name: NSNotification.Name.UITextViewTextDidEndEditing, object: self)
}
/// Removes the Notification handlers.
fileprivate func removeNotificationHandlers() {
let defaultCenter = NotificationCenter.default
defaultCenter.removeObserver(self, name: NSNotification.Name.UITextViewTextDidBeginEditing, object: self)
defaultCenter.removeObserver(self, name: NSNotification.Name.UITextViewTextDidChange, object: self)
defaultCenter.removeObserver(self, name: NSNotification.Name.UITextViewTextDidEndEditing, object: self)
}
}
extension TextView {
/// Updates the placeholderLabel text color.
fileprivate func updatePlaceholderLabelColor() {
tintColor = placeholderActiveColor
placeholderLabel.textColor = isEditing ? placeholderActiveColor : placeholderNormalColor
}
}
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