Commit d1f9360b by Daniel Dahan

development: cleaning up Editor

parent fa08e443
...@@ -19,7 +19,6 @@ ...@@ -19,7 +19,6 @@
9617B0821DFCA8CF00410F8F /* CapturePreview.swift in Headers */ = {isa = PBXBuildFile; fileRef = 96717B0F1DBE6AF600DA84DB /* CapturePreview.swift */; settings = {ATTRIBUTES = (Public, ); }; }; 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, ); }; }; 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, ); }; }; 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, ); }; }; 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, ); }; }; 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, ); }; }; 9617B0881DFCA8CF00410F8F /* PhotoLibrary.swift in Headers */ = {isa = PBXBuildFile; fileRef = 96717B161DBE6B1800DA84DB /* PhotoLibrary.swift */; settings = {ATTRIBUTES = (Public, ); }; };
...@@ -118,7 +117,6 @@ ...@@ -118,7 +117,6 @@
965E81221DD4D5C800D61E4B /* TextView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 96BCB79E1CB40DC500C806FE /* TextView.swift */; }; 965E81221DD4D5C800D61E4B /* TextView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 96BCB79E1CB40DC500C806FE /* TextView.swift */; };
965E81231DD4D7C800D61E4B /* BottomTabBar.swift in Sources */ = {isa = PBXBuildFile; fileRef = 96BCB7591CB40DC500C806FE /* BottomTabBar.swift */; }; 965E81231DD4D7C800D61E4B /* BottomTabBar.swift in Sources */ = {isa = PBXBuildFile; fileRef = 96BCB7591CB40DC500C806FE /* BottomTabBar.swift */; };
965E81241DD4D7C800D61E4B /* Editor.swift in Sources */ = {isa = PBXBuildFile; fileRef = 961DED451DCC40C500F425B6 /* Editor.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 */; }; 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, ); }; }; 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, ); }; }; 9697F7C01D8F2572004741EC /* Material+CALayer.swift in Headers */ = {isa = PBXBuildFile; fileRef = 96F1DC871D654FDF0025F925 /* Material+CALayer.swift */; settings = {ATTRIBUTES = (Public, ); }; };
...@@ -196,7 +194,6 @@ ...@@ -196,7 +194,6 @@
961276621DCD8B1800A7D920 /* CharacterAttribute.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CharacterAttribute.swift; sourceTree = "<group>"; }; 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>"; }; 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>"; }; 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>"; }; 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>"; }; 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>"; }; 961EFC571D738FF600E84652 /* SnackbarController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SnackbarController.swift; sourceTree = "<group>"; };
...@@ -315,7 +312,6 @@ ...@@ -315,7 +312,6 @@
isa = PBXGroup; isa = PBXGroup;
children = ( children = (
961DED451DCC40C500F425B6 /* Editor.swift */, 961DED451DCC40C500F425B6 /* Editor.swift */,
961DED4A1DCC546100F425B6 /* EditorController.swift */,
); );
name = Editor; name = Editor;
sourceTree = "<group>"; sourceTree = "<group>";
...@@ -822,7 +818,6 @@ ...@@ -822,7 +818,6 @@
9617B0821DFCA8CF00410F8F /* CapturePreview.swift in Headers */, 9617B0821DFCA8CF00410F8F /* CapturePreview.swift in Headers */,
9617B0831DFCA8CF00410F8F /* CaptureController.swift in Headers */, 9617B0831DFCA8CF00410F8F /* CaptureController.swift in Headers */,
9617B0841DFCA8CF00410F8F /* Editor.swift in Headers */, 9617B0841DFCA8CF00410F8F /* Editor.swift in Headers */,
9617B0851DFCA8CF00410F8F /* EditorController.swift in Headers */,
9617B0861DFCA8CF00410F8F /* HeightPreset.swift in Headers */, 9617B0861DFCA8CF00410F8F /* HeightPreset.swift in Headers */,
9617B0871DFCA8CF00410F8F /* PageTabBarController.swift in Headers */, 9617B0871DFCA8CF00410F8F /* PageTabBarController.swift in Headers */,
9617B0881DFCA8CF00410F8F /* PhotoLibrary.swift in Headers */, 9617B0881DFCA8CF00410F8F /* PhotoLibrary.swift in Headers */,
...@@ -929,7 +924,6 @@ ...@@ -929,7 +924,6 @@
files = ( files = (
965E81231DD4D7C800D61E4B /* BottomTabBar.swift in Sources */, 965E81231DD4D7C800D61E4B /* BottomTabBar.swift in Sources */,
965E81241DD4D7C800D61E4B /* Editor.swift in Sources */, 965E81241DD4D7C800D61E4B /* Editor.swift in Sources */,
965E81251DD4D7C800D61E4B /* EditorController.swift in Sources */,
961E6BE21DDA2AF3004E6C93 /* Screen.swift in Sources */, 961E6BE21DDA2AF3004E6C93 /* Screen.swift in Sources */,
965E81261DD4D7C800D61E4B /* CharacterAttribute.swift in Sources */, 965E81261DD4D7C800D61E4B /* CharacterAttribute.swift in Sources */,
965E80FF1DD4D5C800D61E4B /* BottomNavigationController.swift in Sources */, 965E80FF1DD4D5C800D61E4B /* BottomNavigationController.swift in Sources */,
......
...@@ -324,8 +324,9 @@ extension Editor { ...@@ -324,8 +324,9 @@ extension Editor {
/// Prepares the keyboard notification center observers. /// Prepares the keyboard notification center observers.
fileprivate func prepareKeyboardNotificationObservers() { fileprivate func prepareKeyboardNotificationObservers() {
NotificationCenter.default.addObserver(self, selector: #selector(handleKeyboardWillShow(notification:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil) let defaultCenter = NotificationCenter.default
NotificationCenter.default.addObserver(self, selector: #selector(handleKeyboardWillHide(notification:)), name: NSNotification.Name.UIKeyboardWillHide, object: nil) 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 {} ...@@ -35,6 +35,14 @@ public protocol TextViewDelegate : UITextViewDelegate {}
@objc(TextView) @objc(TextView)
open class TextView: UITextView { 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 /// A property that accesses the backing layer's background
@IBInspectable @IBInspectable
open override var backgroundColor: UIColor? { open override var backgroundColor: UIColor? {
...@@ -43,56 +51,49 @@ open class TextView: UITextView { ...@@ -43,56 +51,49 @@ open class TextView: UITextView {
} }
} }
/** /// The placeholderLabel font value.
The title UILabel that is displayed when there is text. The @IBInspectable
titleLabel text value is updated with the placeholderLabel open override var font: UIFont? {
text value before being displayed. didSet {
*/ placeholderLabel.font = font
@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 text value.
@IBInspectable
open var placeholder: String? { open var placeholder: String? {
get { get {
return placeholderLabel?.text return placeholderLabel.text
} }
set(value) { 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. /// An override to the text property.
@IBInspectable @IBInspectable
open override var text: String! { open override var text: String? {
didSet { didSet {
handleTextViewTextDidChange() handleTextViewTextDidChange()
} }
...@@ -106,9 +107,9 @@ open class TextView: UITextView { ...@@ -106,9 +107,9 @@ open class TextView: UITextView {
} }
/** /**
Text container UIEdgeInset preset property. This updates the Text container UIEdgeInset preset property. This updates the
textContainerInset property with a preset value. textContainerInset property with a preset value.
*/ */
open var textContainerEdgeInsetsPreset: EdgeInsetsPreset = .none { open var textContainerEdgeInsetsPreset: EdgeInsetsPreset = .none {
didSet { didSet {
textContainerInset = EdgeInsetsPresetToValue(preset: textContainerEdgeInsetsPreset) textContainerInset = EdgeInsetsPresetToValue(preset: textContainerEdgeInsetsPreset)
...@@ -123,39 +124,40 @@ open class TextView: UITextView { ...@@ -123,39 +124,40 @@ open class TextView: UITextView {
} }
/** /**
An initializer that initializes the object with a NSCoder object. An initializer that initializes the object with a NSCoder object.
- Parameter aDecoder: A NSCoder instance. - Parameter aDecoder: A NSCoder instance.
*/ */
public required init?(coder aDecoder: NSCoder) { public required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder) super.init(coder: aDecoder)
prepare() prepare()
} }
/** /**
An initializer that initializes the object with a CGRect object. An initializer that initializes the object with a CGRect object.
If AutoLayout is used, it is better to initilize the instance If AutoLayout is used, it is better to initilize the instance
using the init() initializer. using the init() initializer.
- Parameter frame: A CGRect instance. - Parameter frame: A CGRect instance.
- Parameter textContainer: A NSTextContainer instance. - Parameter textContainer: A NSTextContainer instance.
*/ */
public override init(frame: CGRect, textContainer: NSTextContainer?) { public override init(frame: CGRect, textContainer: NSTextContainer?) {
super.init(frame: frame, textContainer: textContainer) super.init(frame: frame, textContainer: textContainer)
prepare() prepare()
} }
/** /**
A convenience initializer that is mostly used with AutoLayout. A convenience initializer that is mostly used with AutoLayout.
- Parameter textContainer: A NSTextContainer instance. - Parameter textContainer: A NSTextContainer instance.
*/ */
public convenience init(textContainer: NSTextContainer?) { public convenience init(textContainer: NSTextContainer?) {
self.init(frame: .zero, textContainer: textContainer) 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 { deinit {
removeNotificationHandlers() NotificationCenter.default.removeObserver(self)
} }
open override func layoutSubviews() { open override func layoutSubviews() {
...@@ -163,63 +165,36 @@ open class TextView: UITextView { ...@@ -163,63 +165,36 @@ open class TextView: UITextView {
layoutShape() layoutShape()
layoutShadowPath() layoutShadowPath()
placeholderLabel?.preferredMaxLayoutWidth = textContainer.size.width - textContainer.lineFragmentPadding * 2 placeholderLabel.preferredMaxLayoutWidth = textContainer.size.width - textContainer.lineFragmentPadding * 2
titleLabel?.frame.size.width = bounds.width
} }
/// Reloads necessary components when the view has changed. /// Reloads necessary components when the view has changed.
open func reload() { open func reload() {
if let p = placeholderLabel { removeConstraints(constraints)
removeConstraints(constraints) layout(placeholderLabel).edges(
layout(p).edges( top: textContainerInset.top,
top: textContainerInset.top, left: textContainerInset.left + textContainer.lineFragmentPadding,
left: textContainerInset.left + textContainer.lineFragmentPadding, bottom: textContainerInset.bottom,
bottom: textContainerInset.bottom, right: textContainerInset.right + textContainer.lineFragmentPadding)
right: textContainerInset.right + textContainer.lineFragmentPadding)
}
} }
/// Notification handler for when text editing began. /// Notification handler for when text editing began.
@objc @objc
fileprivate func handleTextViewTextDidBegin() { fileprivate func handleTextViewTextDidBegin() {
titleLabel?.textColor = titleLabelActiveColor
} }
/// Notification handler for when text changed. /// Notification handler for when text changed.
@objc @objc
fileprivate func handleTextViewTextDidChange() { fileprivate func handleTextViewTextDidChange() {
if let p = placeholderLabel { placeholderLabel.isHidden = !isEmpty
p.isHidden = !(true == text?.isEmpty)
}
guard let t = text else {
hideTitleLabel()
return
}
if 0 < t.utf16.count {
showTitleLabel()
} else {
hideTitleLabel()
}
} }
/// Notification handler for when text editing ended. /// Notification handler for when text editing ended.
@objc @objc
fileprivate func handleTextViewTextDidEnd() { 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, Prepares the view instance when intialized. When subclassing,
...@@ -232,72 +207,17 @@ open class TextView: UITextView { ...@@ -232,72 +207,17 @@ open class TextView: UITextView {
contentScaleFactor = Screen.scale contentScaleFactor = Screen.scale
textContainerInset = .zero textContainerInset = .zero
backgroundColor = .white backgroundColor = .white
clipsToBounds = false clipsToBounds = false
removeNotificationHandlers() preparePlaceholderLabel()
prepareNotificationHandlers() prepareNotificationHandlers()
reload()
} }
/// prepares the placeholderLabel property. /// prepares the placeholderLabel property.
fileprivate func preparePlaceholderLabel() { fileprivate func preparePlaceholderLabel() {
if let v: UILabel = placeholderLabel { placeholderLabel.font = font
v.font = font placeholderLabel.textAlignment = textAlignment
v.textAlignment = textAlignment placeholderLabel.numberOfLines = 0
v.numberOfLines = 0 placeholderLabel.backgroundColor = .clear
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
}
}
}
} }
/// Prepares the Notification handlers. /// Prepares the Notification handlers.
...@@ -307,12 +227,12 @@ open class TextView: UITextView { ...@@ -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(handleTextViewTextDidChange), name: NSNotification.Name.UITextViewTextDidChange, object: self)
defaultCenter.addObserver(self, selector: #selector(handleTextViewTextDidEnd), name: NSNotification.Name.UITextViewTextDidEndEditing, object: self) defaultCenter.addObserver(self, selector: #selector(handleTextViewTextDidEnd), name: NSNotification.Name.UITextViewTextDidEndEditing, object: self)
} }
}
/// Removes the Notification handlers.
fileprivate func removeNotificationHandlers() { extension TextView {
let defaultCenter = NotificationCenter.default /// Updates the placeholderLabel text color.
defaultCenter.removeObserver(self, name: NSNotification.Name.UITextViewTextDidBeginEditing, object: self) fileprivate func updatePlaceholderLabelColor() {
defaultCenter.removeObserver(self, name: NSNotification.Name.UITextViewTextDidChange, object: self) tintColor = placeholderActiveColor
defaultCenter.removeObserver(self, name: NSNotification.Name.UITextViewTextDidEndEditing, object: self) 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