Commit 18561324 by Daniel Dahan

updated internals for TextView to handle edge cases

parent 511b51ba
...@@ -11,6 +11,8 @@ ...@@ -11,6 +11,8 @@
960590221C388FD800691E88 /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 960590211C388FD800691E88 /* ViewController.swift */; }; 960590221C388FD800691E88 /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 960590211C388FD800691E88 /* ViewController.swift */; };
960590271C388FD800691E88 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 960590261C388FD800691E88 /* Assets.xcassets */; }; 960590271C388FD800691E88 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 960590261C388FD800691E88 /* Assets.xcassets */; };
9605902A1C388FD800691E88 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 960590281C388FD800691E88 /* LaunchScreen.storyboard */; }; 9605902A1C388FD800691E88 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 960590281C388FD800691E88 /* LaunchScreen.storyboard */; };
96607DE61C42CA20008D47D8 /* MaterialKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 96607DE51C42CA20008D47D8 /* MaterialKit.framework */; };
96607DE71C42CA20008D47D8 /* MaterialKit.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = 96607DE51C42CA20008D47D8 /* MaterialKit.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
/* End PBXBuildFile section */ /* End PBXBuildFile section */
/* Begin PBXCopyFilesBuildPhase section */ /* Begin PBXCopyFilesBuildPhase section */
...@@ -20,6 +22,7 @@ ...@@ -20,6 +22,7 @@
dstPath = ""; dstPath = "";
dstSubfolderSpec = 10; dstSubfolderSpec = 10;
files = ( files = (
96607DE71C42CA20008D47D8 /* MaterialKit.framework in Embed Frameworks */,
); );
name = "Embed Frameworks"; name = "Embed Frameworks";
runOnlyForDeploymentPostprocessing = 0; runOnlyForDeploymentPostprocessing = 0;
...@@ -33,6 +36,7 @@ ...@@ -33,6 +36,7 @@
960590261C388FD800691E88 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; }; 960590261C388FD800691E88 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
960590291C388FD800691E88 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = "<group>"; }; 960590291C388FD800691E88 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = "<group>"; };
9605902B1C388FD800691E88 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; }; 9605902B1C388FD800691E88 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
96607DE51C42CA20008D47D8 /* MaterialKit.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; name = MaterialKit.framework; path = "/Users/danieldahan/Library/Developer/Xcode/DerivedData/MaterialKit-gdulktuccbcfwbdfadtpxkworhyc/Build/Products/Debug-iphoneos/MaterialKit.framework"; sourceTree = "<absolute>"; };
/* End PBXFileReference section */ /* End PBXFileReference section */
/* Begin PBXFrameworksBuildPhase section */ /* Begin PBXFrameworksBuildPhase section */
...@@ -40,6 +44,7 @@ ...@@ -40,6 +44,7 @@
isa = PBXFrameworksBuildPhase; isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647; buildActionMask = 2147483647;
files = ( files = (
96607DE61C42CA20008D47D8 /* MaterialKit.framework in Frameworks */,
); );
runOnlyForDeploymentPostprocessing = 0; runOnlyForDeploymentPostprocessing = 0;
}; };
...@@ -49,6 +54,7 @@ ...@@ -49,6 +54,7 @@
960590131C388FD800691E88 = { 960590131C388FD800691E88 = {
isa = PBXGroup; isa = PBXGroup;
children = ( children = (
96607DE51C42CA20008D47D8 /* MaterialKit.framework */,
9605901E1C388FD800691E88 /* TextView */, 9605901E1C388FD800691E88 /* TextView */,
9605901D1C388FD800691E88 /* Products */, 9605901D1C388FD800691E88 /* Products */,
); );
......
...@@ -63,7 +63,7 @@ class ViewController: UIViewController, TextDelegate, TextViewDelegate { ...@@ -63,7 +63,7 @@ class ViewController: UIViewController, TextDelegate, TextViewDelegate {
textView.titleLabel = UILabel() textView.titleLabel = UILabel()
textView.titleLabel!.font = RobotoFont.mediumWithSize(12) textView.titleLabel!.font = RobotoFont.mediumWithSize(12)
textView.titleLabelColor = MaterialColor.grey.lighten2 textView.titleLabelColor = MaterialColor.grey.base
textView.titleLabelActiveColor = MaterialColor.blue.accent3 textView.titleLabelActiveColor = MaterialColor.blue.accent3
view.addSubview(textView) view.addSubview(textView)
......
...@@ -262,6 +262,12 @@ public class TextView: UITextView { ...@@ -262,6 +262,12 @@ public class TextView: UITextView {
} }
} }
/**
A property that sets the distance between the textView and
titleLabel.
*/
public var titleLabelAnimationDistance: CGFloat = 16
/// An override to the text property. /// An override to the text property.
public override var text: String! { public override var text: String! {
didSet { didSet {
...@@ -419,20 +425,17 @@ public class TextView: UITextView { ...@@ -419,20 +425,17 @@ public class TextView: UITextView {
/// Notification handler for when text editing began. /// Notification handler for when text editing began.
internal func handleTextViewTextDidBegin() { internal func handleTextViewTextDidBegin() {
prepareTitleLabelForAnimation() titleLabel?.textColor = titleLabelActiveColor
} }
/// Notification handler for when text changed. /// Notification handler for when text changed.
internal func handleTextViewTextDidChange() { internal func handleTextViewTextDidChange() {
prepareTitleLabelForAnimation()
if let p = placeholderLabel { if let p = placeholderLabel {
p.hidden = !(true == text?.isEmpty) p.hidden = !(true == text?.isEmpty)
} }
if 0 < text?.utf16.count { if 0 < text?.utf16.count {
showTitleLabel() showTitleLabel()
titleLabel?.textColor = titleLabelActiveColor
} else if 0 == text?.utf16.count { } else if 0 == text?.utf16.count {
hideTitleLabel() hideTitleLabel()
} }
...@@ -463,7 +466,7 @@ public class TextView: UITextView { ...@@ -463,7 +466,7 @@ public class TextView: UITextView {
when subclassing. when subclassing.
*/ */
private func prepareView() { private func prepareView() {
textContainerInset = UIEdgeInsets(top: 8, left: 0, bottom: 8, right: 0) textContainerInset = UIEdgeInsets(top: 0, left: 0, bottom: 16, right: 0)
backgroundColor = MaterialColor.white backgroundColor = MaterialColor.white
masksToBounds = false masksToBounds = false
removeNotificationHandlers() removeNotificationHandlers()
...@@ -488,52 +491,47 @@ public class TextView: UITextView { ...@@ -488,52 +491,47 @@ public class TextView: UITextView {
/// Prepares the titleLabel property. /// Prepares the titleLabel property.
private func prepareTitleLabel() { private func prepareTitleLabel() {
if let v: UILabel = titleLabel { if let v: UILabel = titleLabel {
MaterialAnimation.animationDisabled { v.hidden = true
v.hidden = true addSubview(v)
if 0 < text?.utf16.count {
showTitleLabel()
} else {
v.alpha = 0 v.alpha = 0
} }
addSubview(v)
} }
} }
/// Parepares the titleLabel before it animates. /// Shows and animates the titleLabel property.
private func prepareTitleLabelForAnimation() { private func showTitleLabel() {
if let v: UILabel = titleLabel { if let v: UILabel = titleLabel {
if v.hidden { if v.hidden {
let h: CGFloat = v.font.pointSize
v.frame = CGRectMake(0, -h, bounds.width, h)
if let s: String = placeholderLabel?.text { if let s: String = placeholderLabel?.text {
v.text = s if 0 == v.text?.utf16.count || nil == v.text {
} v.text = s
if 0 == text?.utf16.count { }
v.textColor = titleLabelColor
} else {
v.textColor = titleLabelActiveColor
} }
let h: CGFloat = v.font.pointSize
v.frame = CGRectMake(0, -h, bounds.width, h)
v.hidden = false
UIView.animateWithDuration(0.25, animations: {
v.alpha = 1
print(v.frame)
v.frame.origin.y = -v.frame.height - self.titleLabelAnimationDistance
})
} }
} }
} }
/// Shows and animates the titleLabel property.
private func showTitleLabel() {
if let v: UILabel = titleLabel {
v.hidden = false
UIView.animateWithDuration(0.25, animations: {
v.alpha = 1
print(v.frame)
v.frame.origin.y = -v.frame.height - 4
})
}
}
/// Hides and animates the titleLabel property. /// Hides and animates the titleLabel property.
private func hideTitleLabel() { private func hideTitleLabel() {
if let v: UILabel = titleLabel { if let v: UILabel = titleLabel {
UIView.animateWithDuration(0.25, animations: { if !v.hidden {
v.alpha = 0 UIView.animateWithDuration(0.25, animations: {
v.frame.origin.y = -v.frame.height v.alpha = 0
}) { _ in v.frame.origin.y = -v.frame.height
v.hidden = true }) { _ in
v.hidden = true
}
} }
} }
} }
......
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