Commit 18561324 by Daniel Dahan

updated internals for TextView to handle edge cases

parent 511b51ba
......@@ -11,6 +11,8 @@
960590221C388FD800691E88 /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 960590211C388FD800691E88 /* ViewController.swift */; };
960590271C388FD800691E88 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 960590261C388FD800691E88 /* Assets.xcassets */; };
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 */
/* Begin PBXCopyFilesBuildPhase section */
......@@ -20,6 +22,7 @@
dstPath = "";
dstSubfolderSpec = 10;
files = (
96607DE71C42CA20008D47D8 /* MaterialKit.framework in Embed Frameworks */,
);
name = "Embed Frameworks";
runOnlyForDeploymentPostprocessing = 0;
......@@ -33,6 +36,7 @@
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>"; };
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 */
/* Begin PBXFrameworksBuildPhase section */
......@@ -40,6 +44,7 @@
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
96607DE61C42CA20008D47D8 /* MaterialKit.framework in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
};
......@@ -49,6 +54,7 @@
960590131C388FD800691E88 = {
isa = PBXGroup;
children = (
96607DE51C42CA20008D47D8 /* MaterialKit.framework */,
9605901E1C388FD800691E88 /* TextView */,
9605901D1C388FD800691E88 /* Products */,
);
......
......@@ -63,7 +63,7 @@ class ViewController: UIViewController, TextDelegate, TextViewDelegate {
textView.titleLabel = UILabel()
textView.titleLabel!.font = RobotoFont.mediumWithSize(12)
textView.titleLabelColor = MaterialColor.grey.lighten2
textView.titleLabelColor = MaterialColor.grey.base
textView.titleLabelActiveColor = MaterialColor.blue.accent3
view.addSubview(textView)
......
......@@ -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.
public override var text: String! {
didSet {
......@@ -419,20 +425,17 @@ public class TextView: UITextView {
/// Notification handler for when text editing began.
internal func handleTextViewTextDidBegin() {
prepareTitleLabelForAnimation()
titleLabel?.textColor = titleLabelActiveColor
}
/// Notification handler for when text changed.
internal func handleTextViewTextDidChange() {
prepareTitleLabelForAnimation()
if let p = placeholderLabel {
p.hidden = !(true == text?.isEmpty)
}
if 0 < text?.utf16.count {
showTitleLabel()
titleLabel?.textColor = titleLabelActiveColor
} else if 0 == text?.utf16.count {
hideTitleLabel()
}
......@@ -463,7 +466,7 @@ public class TextView: UITextView {
when subclassing.
*/
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
masksToBounds = false
removeNotificationHandlers()
......@@ -488,52 +491,47 @@ public class TextView: UITextView {
/// Prepares the titleLabel property.
private func prepareTitleLabel() {
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
}
addSubview(v)
}
}
/// Parepares the titleLabel before it animates.
private func prepareTitleLabelForAnimation() {
/// Shows and animates the titleLabel property.
private func showTitleLabel() {
if let v: UILabel = titleLabel {
if v.hidden {
let h: CGFloat = v.font.pointSize
v.frame = CGRectMake(0, -h, bounds.width, h)
if let s: String = placeholderLabel?.text {
v.text = s
}
if 0 == text?.utf16.count {
v.textColor = titleLabelColor
} else {
v.textColor = titleLabelActiveColor
if 0 == v.text?.utf16.count || nil == v.text {
v.text = s
}
}
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.
private func hideTitleLabel() {
if let v: UILabel = titleLabel {
UIView.animateWithDuration(0.25, animations: {
v.alpha = 0
v.frame.origin.y = -v.frame.height
}) { _ in
v.hidden = true
if !v.hidden {
UIView.animateWithDuration(0.25, animations: {
v.alpha = 0
v.frame.origin.y = -v.frame.height
}) { _ 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