Commit bc0d00c7 by Daniel Dahan

initil reorganization of TextView

parent 53092d1d
......@@ -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 */; };
96977E161CBD7F59000BEFC4 /* Material.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 96977E151CBD7F59000BEFC4 /* Material.framework */; };
96977E171CBD7F59000BEFC4 /* Material.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = 96977E151CBD7F59000BEFC4 /* Material.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
/* End PBXBuildFile section */
/* Begin PBXCopyFilesBuildPhase section */
......@@ -20,6 +22,7 @@
dstPath = "";
dstSubfolderSpec = 10;
files = (
96977E171CBD7F59000BEFC4 /* Material.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>"; };
96977E151CBD7F59000BEFC4 /* Material.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; name = Material.framework; path = "/Users/danieldahan/Library/Developer/Xcode/DerivedData/Material-bshwisidfobstlbomegqyybbrygh/Build/Products/Debug-iphoneos/Material.framework"; sourceTree = "<absolute>"; };
/* End PBXFileReference section */
/* Begin PBXFrameworksBuildPhase section */
......@@ -40,6 +44,7 @@
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
96977E161CBD7F59000BEFC4 /* Material.framework in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
};
......@@ -49,6 +54,7 @@
960590131C388FD800691E88 = {
isa = PBXGroup;
children = (
96977E151CBD7F59000BEFC4 /* Material.framework */,
9605901E1C388FD800691E88 /* TextView */,
9605901D1C388FD800691E88 /* Products */,
);
......
......@@ -56,28 +56,13 @@ class ViewController: UIViewController, TextDelegate {
/// Prepares the textView.
func prepareTextView() {
let layoutManager: NSLayoutManager = NSLayoutManager()
let textContainer: NSTextContainer = NSTextContainer(size: view.bounds.size)
layoutManager.addTextContainer(textContainer)
text.delegate = self
text.textStorage.addLayoutManager(layoutManager)
textView = TextView(textContainer: textContainer)
textView.font = RobotoFont.regular
textView.placeholderLabel = UILabel()
textView.placeholderLabel!.textColor = MaterialColor.grey.base
textView.placeholderLabel!.text = "Description"
textView.titleLabel = UILabel()
textView.titleLabel!.font = RobotoFont.mediumWithSize(12)
textView.titleLabelColor = MaterialColor.grey.base
textView.titleLabelActiveColor = MaterialColor.blue.accent3
textView = TextView()
// textView.font = RobotoFont.regular
textView.backgroundColor = MaterialColor.grey.base
view.addSubview(textView)
textView!.translatesAutoresizingMaskIntoConstraints = false
MaterialLayout.alignToParent(view, child: textView!, top: 124, left: 24, bottom: 24, right: 24)
MaterialLayout.alignToParent(view, child: textView!, top: 124, left: 24, bottom: 200, right: 24)
}
/**
......@@ -97,4 +82,3 @@ class ViewController: UIViewController, TextDelegate {
textStorage.addAttribute(NSFontAttributeName, value: UIFont.boldSystemFontOfSize(16), range: result!.range)
}
}
......@@ -13,6 +13,7 @@
9660162A1CB2F04E00AAB661 /* Material.h in Headers */ = {isa = PBXBuildFile; fileRef = 96D88C091C1328D800B91418 /* Material.h */; settings = {ATTRIBUTES = (Public, ); }; };
96815B381CA07BA20006CBE2 /* MaterialViewTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 967887881C9777CB0037F6C9 /* MaterialViewTests.swift */; };
96977DA61CBB2E49000BEFC4 /* Material+UIImage+FilterBlur.swift in Sources */ = {isa = PBXBuildFile; fileRef = 96977DA51CBB2E49000BEFC4 /* Material+UIImage+FilterBlur.swift */; };
96977E191CBDA44B000BEFC4 /* MaterialTextView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 96977E181CBDA44B000BEFC4 /* MaterialTextView.swift */; };
96BCB7A11CB40DC500C806FE /* BottomNavigationController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 96BCB7581CB40DC500C806FE /* BottomNavigationController.swift */; };
96BCB7A21CB40DC500C806FE /* BottomTabBar.swift in Sources */ = {isa = PBXBuildFile; fileRef = 96BCB7591CB40DC500C806FE /* BottomTabBar.swift */; };
96BCB7A31CB40DC500C806FE /* CapturePreview.swift in Sources */ = {isa = PBXBuildFile; fileRef = 96BCB75A1CB40DC500C806FE /* CapturePreview.swift */; };
......@@ -199,6 +200,7 @@
9660161C1CB2ED6C00AAB661 /* Material OSX Tests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = "Material OSX Tests.xctest"; sourceTree = BUILT_PRODUCTS_DIR; };
967887881C9777CB0037F6C9 /* MaterialViewTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MaterialViewTests.swift; sourceTree = "<group>"; };
96977DA51CBB2E49000BEFC4 /* Material+UIImage+FilterBlur.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "Material+UIImage+FilterBlur.swift"; sourceTree = "<group>"; };
96977E181CBDA44B000BEFC4 /* MaterialTextView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MaterialTextView.swift; sourceTree = "<group>"; };
96BCB7581CB40DC500C806FE /* BottomNavigationController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = BottomNavigationController.swift; sourceTree = "<group>"; };
96BCB7591CB40DC500C806FE /* BottomTabBar.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = BottomTabBar.swift; sourceTree = "<group>"; };
96BCB75A1CB40DC500C806FE /* CapturePreview.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CapturePreview.swift; sourceTree = "<group>"; };
......@@ -577,6 +579,7 @@
96BCB79C1CB40DC500C806FE /* TextField.swift */,
96BCB79D1CB40DC500C806FE /* TextStorage.swift */,
96BCB79E1CB40DC500C806FE /* TextView.swift */,
96977E181CBDA44B000BEFC4 /* MaterialTextView.swift */,
);
name = Text;
sourceTree = "<group>";
......@@ -863,6 +866,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
96977E191CBDA44B000BEFC4 /* MaterialTextView.swift in Sources */,
96BCB7C01CB40DC500C806FE /* MaterialDataSourceItem.swift in Sources */,
96BCB7AC1CB40DC500C806FE /* Material+Obj-C.swift in Sources */,
96BCB7BB1CB40DC500C806FE /* MaterialCollectionViewCell.swift in Sources */,
......
......@@ -92,7 +92,7 @@ public class BottomTabBar : UITabBar {
*/
@IBInspectable public var width: CGFloat {
get {
return layer.frame.size.width
return layer.frame.width
}
set(value) {
layer.frame.size.width = value
......@@ -107,7 +107,7 @@ public class BottomTabBar : UITabBar {
*/
@IBInspectable public var height: CGFloat {
get {
return layer.frame.size.height
return layer.frame.height
}
set(value) {
layer.frame.size.height = value
......
......@@ -111,7 +111,7 @@ public class MaterialButton : UIButton {
*/
@IBInspectable public var width: CGFloat {
get {
return layer.frame.size.width
return layer.frame.width
}
set(value) {
layer.frame.size.width = value
......@@ -129,7 +129,7 @@ public class MaterialButton : UIButton {
*/
@IBInspectable public var height: CGFloat {
get {
return layer.frame.size.height
return layer.frame.height
}
set(value) {
layer.frame.size.height = value
......
......@@ -218,7 +218,7 @@ public class MaterialCollectionViewCell : UICollectionViewCell {
*/
@IBInspectable public var width: CGFloat {
get {
return layer.frame.size.width
return layer.frame.width
}
set(value) {
layer.frame.size.width = value
......@@ -236,7 +236,7 @@ public class MaterialCollectionViewCell : UICollectionViewCell {
*/
@IBInspectable public var height: CGFloat {
get {
return layer.frame.size.height
return layer.frame.height
}
set(value) {
layer.frame.size.height = value
......
......@@ -97,7 +97,7 @@ public class MaterialSwitch : UIControl {
/// A property that accesses the layer.frame.size.width property.
@IBInspectable public var width: CGFloat {
get {
return layer.frame.size.width
return layer.frame.width
}
set(value) {
layer.frame.size.width = value
......@@ -107,7 +107,7 @@ public class MaterialSwitch : UIControl {
/// A property that accesses the layer.frame.size.height property.
@IBInspectable public var height: CGFloat {
get {
return layer.frame.size.height
return layer.frame.height
}
set(value) {
layer.frame.size.height = value
......
......@@ -110,7 +110,7 @@ public class MaterialTableViewCell : UITableViewCell {
*/
@IBInspectable public var width: CGFloat {
get {
return layer.frame.size.width
return layer.frame.width
}
set(value) {
layer.frame.size.width = value
......@@ -125,7 +125,7 @@ public class MaterialTableViewCell : UITableViewCell {
*/
@IBInspectable public var height: CGFloat {
get {
return layer.frame.size.height
return layer.frame.height
}
set(value) {
layer.frame.size.height = value
......
/*
* Copyright (C) 2015 - 2016, Daniel Dahan and CosmicMind, Inc. <http://cosmicmind.io>.
* 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 Material 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
@objc(MaterialTextViewDelegate)
public protocol MaterialTextViewDelegate : UITextViewDelegate {}
@IBDesignable
@objc(MaterialTextView)
public class MaterialTextView: UITextView {
/**
This property is the same as clipsToBounds. It crops any of the view's
contents from bleeding past the view's frame.
*/
@IBInspectable public var masksToBounds: Bool {
get {
return layer.masksToBounds
}
set(value) {
layer.masksToBounds = value
}
}
/// A property that accesses the backing layer's backgroundColor.
@IBInspectable public override var backgroundColor: UIColor? {
didSet {
layer.backgroundColor = backgroundColor?.CGColor
}
}
/// A property that accesses the layer.frame.origin.x property.
@IBInspectable public var x: CGFloat {
get {
return layer.frame.origin.x
}
set(value) {
layer.frame.origin.x = value
}
}
/// A property that accesses the layer.frame.origin.y property.
@IBInspectable public var y: CGFloat {
get {
return layer.frame.origin.y
}
set(value) {
layer.frame.origin.y = value
}
}
/// A property that accesses the layer.frame.size.width property.
@IBInspectable public var width: CGFloat {
get {
return layer.frame.width
}
set(value) {
layer.frame.size.width = value
}
}
/// A property that accesses the layer.frame.size.height property.
@IBInspectable public var height: CGFloat {
get {
return layer.frame.height
}
set(value) {
layer.frame.size.height = value
}
}
/// A property that accesses the backing layer's shadowColor.
@IBInspectable public var shadowColor: UIColor? {
didSet {
layer.shadowColor = shadowColor?.CGColor
}
}
/// A property that accesses the backing layer's shadowOffset.
@IBInspectable public var shadowOffset: CGSize {
get {
return layer.shadowOffset
}
set(value) {
layer.shadowOffset = value
}
}
/// A property that accesses the backing layer's shadowOpacity.
@IBInspectable public var shadowOpacity: Float {
get {
return layer.shadowOpacity
}
set(value) {
layer.shadowOpacity = value
}
}
/// A property that accesses the backing layer's shadowRadius.
@IBInspectable public var shadowRadius: CGFloat {
get {
return layer.shadowRadius
}
set(value) {
layer.shadowRadius = value
}
}
/// A property that accesses the backing layer's shadowPath.
@IBInspectable public var shadowPath: CGPath? {
get {
return layer.shadowPath
}
set(value) {
layer.shadowPath = value
}
}
/// Enables automatic shadowPath sizing.
@IBInspectable public var shadowPathAutoSizeEnabled: Bool = true {
didSet {
if shadowPathAutoSizeEnabled {
layoutShadowPath()
} else {
shadowPath = nil
}
}
}
/**
A property that sets the shadowOffset, shadowOpacity, and shadowRadius
for the backing layer. This is the preferred method of setting depth
in order to maintain consitency across UI objects.
*/
public var depth: MaterialDepth = .None {
didSet {
let value: MaterialDepthType = MaterialDepthToValue(depth)
shadowOffset = value.offset
shadowOpacity = value.opacity
shadowRadius = value.radius
layoutShadowPath()
}
}
/// A property that sets the cornerRadius of the backing layer.
public var cornerRadiusPreset: MaterialRadius = .None {
didSet {
if let v: MaterialRadius = cornerRadiusPreset {
cornerRadius = MaterialRadiusToValue(v)
}
}
}
/// A property that accesses the layer.cornerRadius.
@IBInspectable public var cornerRadius: CGFloat {
get {
return layer.cornerRadius
}
set(value) {
layer.cornerRadius = value
layoutShadowPath()
}
}
/// A preset property to set the borderWidth.
public var borderWidthPreset: MaterialBorder = .None {
didSet {
borderWidth = MaterialBorderToValue(borderWidthPreset)
}
}
/// A property that accesses the layer.borderWith.
@IBInspectable public var borderWidth: CGFloat {
get {
return layer.borderWidth
}
set(value) {
layer.borderWidth = value
}
}
/// A property that accesses the layer.borderColor property.
@IBInspectable public var borderColor: UIColor? {
get {
return nil == layer.borderColor ? nil : UIColor(CGColor: layer.borderColor!)
}
set(value) {
layer.borderColor = value?.CGColor
}
}
/// A property that accesses the layer.position property.
@IBInspectable public var position: CGPoint {
get {
return layer.position
}
set(value) {
layer.position = value
}
}
/// A property that accesses the layer.zPosition property.
@IBInspectable public var zPosition: CGFloat {
get {
return layer.zPosition
}
set(value) {
layer.zPosition = value
}
}
/**
Text container UIEdgeInset preset property. This updates the
textContainerInset property with a preset value.
*/
public var textContainerInsetPreset: MaterialEdgeInset = .None {
didSet {
textContainerInset = MaterialEdgeInsetToValue(textContainerInsetPreset)
}
}
/**
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)
prepareView()
}
/**
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?) {
let lm: NSLayoutManager = NSLayoutManager()
let tc: NSTextContainer = nil == textContainer ? NSTextContainer(size: CGSizeZero) : textContainer!
let ts: NSTextStorage = NSTextStorage()
lm.addTextContainer(tc)
ts.addLayoutManager(lm)
super.init(frame: frame, textContainer: tc)
prepareView()
}
/**
A convenience initializer that is mostly used with AutoLayout.
*/
public convenience init() {
self.init(frame: CGRectNull, textContainer: nil)
}
/**
A convenience initializer that is mostly used with AutoLayout.
- Parameter textContainer: A NSTextContainer instance.
*/
public convenience init(textContainer: NSTextContainer) {
self.init(frame: CGRectNull, textContainer: textContainer)
}
/**
A convenience initializer.
- Parameter frame: A CGRect defining the frame of the view.
*/
public convenience init(frame: CGRect) {
self.init(frame: frame, textContainer: nil)
}
/** Denitializer. This should never be called unless you know
what you are doing.
*/
deinit {
removeNotificationHandlers()
}
/// Overriding the layout callback for sublayers.
public override func layoutSublayersOfLayer(layer: CALayer) {
super.layoutSublayersOfLayer(layer)
if self.layer == layer {
layoutShadowPath()
}
}
/**
A method that accepts CAAnimation objects and executes them on the
view's backing layer.
- Parameter animation: A CAAnimation instance.
*/
public func animate(animation: CAAnimation) {
animation.delegate = self
if let a: CABasicAnimation = animation as? CABasicAnimation {
a.fromValue = (nil == layer.presentationLayer() ? layer : layer.presentationLayer() as! CALayer).valueForKeyPath(a.keyPath!)
}
if let a: CAPropertyAnimation = animation as? CAPropertyAnimation {
layer.addAnimation(a, forKey: a.keyPath!)
} else if let a: CAAnimationGroup = animation as? CAAnimationGroup {
layer.addAnimation(a, forKey: nil)
} else if let a: CATransition = animation as? CATransition {
layer.addAnimation(a, forKey: kCATransition)
}
}
/**
A delegation method that is executed when the backing layer starts
running an animation.
- Parameter anim: The currently running CAAnimation instance.
*/
public override func animationDidStart(anim: CAAnimation) {
(delegate as? MaterialAnimationDelegate)?.materialAnimationDidStart?(anim)
}
/**
A delegation method that is executed when the backing layer stops
running an animation.
- Parameter anim: The CAAnimation instance that stopped running.
- Parameter flag: A boolean that indicates if the animation stopped
because it was completed or interrupted. True if completed, false
if interrupted.
*/
public override func animationDidStop(anim: CAAnimation, finished flag: Bool) {
if let a: CAPropertyAnimation = anim as? CAPropertyAnimation {
if let b: CABasicAnimation = a as? CABasicAnimation {
if let v: AnyObject = b.toValue {
if let k: String = b.keyPath {
layer.setValue(v, forKeyPath: k)
layer.removeAnimationForKey(k)
}
}
}
(delegate as? MaterialAnimationDelegate)?.materialAnimationDidStop?(anim, finished: flag)
} else if let a: CAAnimationGroup = anim as? CAAnimationGroup {
for x in a.animations! {
animationDidStop(x, finished: true)
}
}
}
/// Notification handler for when text editing began.
internal func handleTextViewTextDidBegin() {
}
/// Notification handler for when text changed.
internal func handleTextViewTextDidChange() {
}
/// Notification handler for when text editing ended.
internal func handleTextViewTextDidEnd() {
}
/// Sets the shadow path.
internal func layoutShadowPath() {
if shadowPathAutoSizeEnabled {
if .None == depth {
shadowPath = nil
} else if nil == shadowPath {
shadowPath = UIBezierPath(roundedRect: bounds, cornerRadius: cornerRadius).CGPath
} else {
animate(MaterialAnimation.shadowPath(UIBezierPath(roundedRect: bounds, cornerRadius: cornerRadius).CGPath, duration: 0))
}
}
}
/**
Prepares the view instance when intialized. When subclassing,
it is recommended to override the prepareView method
to initialize property values and other setup operations.
The super.prepareView method should always be called immediately
when subclassing.
*/
private func prepareView() {
textContainerInsetPreset = .Square3
backgroundColor = MaterialColor.white
textColor = MaterialColor.darkText.primary
removeNotificationHandlers()
prepareNotificationHandlers()
}
/// Prepares the Notification handlers.
private func prepareNotificationHandlers() {
let dc: NSNotificationCenter = NSNotificationCenter.defaultCenter()
dc.addObserver(self, selector: #selector(handleTextViewTextDidBegin), name: UITextViewTextDidBeginEditingNotification, object: nil)
dc.addObserver(self, selector: #selector(handleTextViewTextDidChange), name: UITextViewTextDidChangeNotification, object: nil)
dc.addObserver(self, selector: #selector(handleTextViewTextDidEnd), name: UITextViewTextDidEndEditingNotification, object: nil)
}
/// Removes the Notification handlers.
private func removeNotificationHandlers() {
let dc: NSNotificationCenter = NSNotificationCenter.defaultCenter()
dc.removeObserver(self, name: UITextViewTextDidBeginEditingNotification, object: nil)
dc.removeObserver(self, name: UITextViewTextDidChangeNotification, object: nil)
dc.removeObserver(self, name: UITextViewTextDidEndEditingNotification, object: nil)
}
}
......@@ -166,7 +166,7 @@ public class MaterialView : UIView {
*/
@IBInspectable public var width: CGFloat {
get {
return layer.frame.size.width
return layer.frame.width
}
set(value) {
layer.frame.size.width = value
......@@ -184,7 +184,7 @@ public class MaterialView : UIView {
*/
@IBInspectable public var height: CGFloat {
get {
return layer.frame.size.height
return layer.frame.height
}
set(value) {
layer.frame.size.height = value
......
......@@ -134,7 +134,7 @@ public class NavigationBar : UINavigationBar {
*/
@IBInspectable public var width: CGFloat {
get {
return layer.frame.size.width
return layer.frame.width
}
set(value) {
layer.frame.size.width = value
......@@ -149,7 +149,7 @@ public class NavigationBar : UINavigationBar {
*/
@IBInspectable public var height: CGFloat {
get {
return layer.frame.size.height
return layer.frame.height
}
set(value) {
layer.frame.size.height = value
......
......@@ -80,7 +80,7 @@ public class TextField : UITextField {
/// A property that accesses the layer.frame.size.width property.
@IBInspectable public var width: CGFloat {
get {
return layer.frame.size.width
return layer.frame.width
}
set(value) {
layer.frame.size.width = value
......@@ -90,7 +90,7 @@ public class TextField : UITextField {
/// A property that accesses the layer.frame.size.height property.
@IBInspectable public var height: CGFloat {
get {
return layer.frame.size.height
return layer.frame.height
}
set(value) {
layer.frame.size.height = value
......
......@@ -35,269 +35,100 @@ public protocol TextViewDelegate : UITextViewDelegate {}
@IBDesignable
@objc(TextView)
public class TextView: UITextView {
public class TextView: MaterialView {
/// Reference to the textView.
public private(set) var textView: MaterialTextView!
/**
This property is the same as clipsToBounds. It crops any of the view's
contents from bleeding past the view's frame.
Text container UIEdgeInset preset property. This updates the
textContainerInset property with a preset value.
*/
@IBInspectable public var masksToBounds: Bool {
get {
return layer.masksToBounds
}
set(value) {
layer.masksToBounds = value
}
}
/// A property that accesses the backing layer's backgroundColor.
@IBInspectable public override var backgroundColor: UIColor? {
didSet {
layer.backgroundColor = backgroundColor?.CGColor
}
}
/// A property that accesses the layer.frame.origin.x property.
@IBInspectable public var x: CGFloat {
get {
return layer.frame.origin.x
}
set(value) {
layer.frame.origin.x = value
}
}
/// A property that accesses the layer.frame.origin.y property.
@IBInspectable public var y: CGFloat {
get {
return layer.frame.origin.y
}
set(value) {
layer.frame.origin.y = value
}
}
/// A property that accesses the layer.frame.size.width property.
@IBInspectable public var width: CGFloat {
get {
return layer.frame.size.width
}
set(value) {
layer.frame.size.width = value
}
}
/// A property that accesses the layer.frame.size.height property.
@IBInspectable public var height: CGFloat {
get {
return layer.frame.size.height
}
set(value) {
layer.frame.size.height = value
}
}
/// A property that accesses the backing layer's shadowColor.
@IBInspectable public var shadowColor: UIColor? {
didSet {
layer.shadowColor = shadowColor?.CGColor
}
}
/// A property that accesses the backing layer's shadowOffset.
@IBInspectable public var shadowOffset: CGSize {
get {
return layer.shadowOffset
}
set(value) {
layer.shadowOffset = value
}
}
/// A property that accesses the backing layer's shadowOpacity.
@IBInspectable public var shadowOpacity: Float {
public var textContainerInsetPreset: MaterialEdgeInset {
get {
return layer.shadowOpacity
return textView.textContainerInsetPreset
}
set(value) {
layer.shadowOpacity = value
textView.textContainerInsetPreset = value
}
}
/// A property that accesses the backing layer's shadowRadius.
@IBInspectable public var shadowRadius: CGFloat {
get {
return layer.shadowRadius
}
set(value) {
layer.shadowRadius = value
}
}
/// A property that accesses the backing layer's shadowPath.
@IBInspectable public var shadowPath: CGPath? {
/// Text container UIEdgeInset property.
public var textContainerInset: UIEdgeInsets {
get {
return layer.shadowPath
return textView.textContainerInset
}
set(value) {
layer.shadowPath = value
}
}
/// Enables automatic shadowPath sizing.
@IBInspectable public var shadowPathAutoSizeEnabled: Bool = true {
didSet {
if shadowPathAutoSizeEnabled {
layoutShadowPath()
} else {
shadowPath = nil
}
textView.textContainerInset = value
}
}
/**
A property that sets the shadowOffset, shadowOpacity, and shadowRadius
for the backing layer. This is the preferred method of setting depth
in order to maintain consitency across UI objects.
The title UILabel that is displayed when there is text. The
titleLabel text value is updated with the placeholder text
value before being displayed.
*/
public var depth: MaterialDepth = .None {
didSet {
let value: MaterialDepthType = MaterialDepthToValue(depth)
shadowOffset = value.offset
shadowOpacity = value.opacity
shadowRadius = value.radius
layoutShadowPath()
}
}
@IBInspectable public private(set) var titleLabel: UILabel!
/// A property that sets the cornerRadius of the backing layer.
public var cornerRadiusPreset: MaterialRadius = .None {
/// The color of the titleLabel text when the textField is not active.
@IBInspectable public var titleLabelColor: UIColor? {
didSet {
if let v: MaterialRadius = cornerRadiusPreset {
cornerRadius = MaterialRadiusToValue(v)
titleLabel.textColor = titleLabelColor
if nil == lineLayerColor {
lineLayerColor = titleLabelColor
}
}
}
/// A property that accesses the layer.cornerRadius.
@IBInspectable public var cornerRadius: CGFloat {
get {
return layer.cornerRadius
}
set(value) {
layer.cornerRadius = value
layoutShadowPath()
}
}
/// A preset property to set the borderWidth.
public var borderWidthPreset: MaterialBorder = .None {
/// The color of the titleLabel text when the textField is active.
@IBInspectable public var titleLabelActiveColor: UIColor? {
didSet {
borderWidth = MaterialBorderToValue(borderWidthPreset)
}
}
/// A property that accesses the layer.borderWith.
@IBInspectable public var borderWidth: CGFloat {
get {
return layer.borderWidth
}
set(value) {
layer.borderWidth = value
}
}
/// A property that accesses the layer.borderColor property.
@IBInspectable public var borderColor: UIColor? {
get {
return nil == layer.borderColor ? nil : UIColor(CGColor: layer.borderColor!)
}
set(value) {
layer.borderColor = value?.CGColor
}
}
/// A property that accesses the layer.position property.
@IBInspectable public var position: CGPoint {
get {
return layer.position
}
set(value) {
layer.position = value
}
}
/// A property that accesses the layer.zPosition property.
@IBInspectable public var zPosition: CGFloat {
get {
return layer.zPosition
}
set(value) {
layer.zPosition = value
if nil == lineLayerActiveColor {
lineLayerActiveColor = titleLabelActiveColor
}
tintColor = titleLabelActiveColor
}
}
/**
The title UILabel that is displayed when there is text. The
titleLabel text value is updated with the placeholderLabel
text value before being displayed.
A property that sets the distance between the textField and
titleLabel.
*/
public var titleLabel: UILabel? {
didSet {
prepareTitleLabel()
}
}
/// The color of the titleLabel text when the textView is not active.
@IBInspectable public var titleLabelColor: UIColor? {
didSet {
titleLabel?.textColor = titleLabelColor
}
}
@IBInspectable public var titleLabelAnimationDistance: CGFloat = 4
/// The color of the titleLabel text when the textView is active.
@IBInspectable public var titleLabelActiveColor: UIColor?
/// The bottom border layer.
public private(set) lazy var lineLayer: CAShapeLayer = CAShapeLayer()
/**
A property that sets the distance between the textView and
titleLabel.
A property that sets the distance between the textField and
lineLayer.
*/
@IBInspectable public var titleLabelAnimationDistance: CGFloat = 8
@IBInspectable public var lineLayerDistance: CGFloat = 4
/// Placeholder UILabel view.
public var placeholderLabel: UILabel? {
/// The lineLayer color when inactive.
@IBInspectable public var lineLayerColor: UIColor? {
didSet {
preparePlaceholderLabel()
lineLayer.backgroundColor = lineLayerColor?.CGColor
}
}
/// An override to the text property.
@IBInspectable public override var text: String! {
didSet {
handleTextViewTextDidChange()
}
}
/// An override to the attributedText property.
public override var attributedText: NSAttributedString! {
didSet {
handleTextViewTextDidChange()
}
}
/// The lineLayer active color.
@IBInspectable public var lineLayerActiveColor: UIColor?
/**
Text container UIEdgeInset preset property. This updates the
textContainerInset property with a preset value.
*/
public var textContainerInsetPreset: MaterialEdgeInset = .None {
/// Sets the placeholder value.
@IBInspectable public var placeholder: String? {
didSet {
textContainerInset = MaterialEdgeInsetToValue(textContainerInsetPreset)
if let v: String = placeholder {
// attributedText = NSAttributedString(string: v, attributes: [NSForegroundColorAttributeName: placeholderTextColor])
}
}
}
/// Text container UIEdgeInset property.
public override var textContainerInset: UIEdgeInsets {
/// Placeholder textColor.
@IBInspectable public var placeholderTextColor: UIColor = MaterialColor.darkText.others {
didSet {
reloadView()
if let v: String = placeholder {
// attributedText = NSAttributedString(string: v, attributes: [NSForegroundColorAttributeName: placeholderTextColor])
}
}
}
......@@ -310,153 +141,15 @@ public class TextView: UITextView {
prepareView()
}
/**
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)
prepareView()
}
/**
A convenience initializer that is mostly used with AutoLayout.
- Parameter textContainer: A NSTextContainer instance.
*/
public convenience init(textContainer: NSTextContainer?) {
self.init(frame: CGRectNull, textContainer: textContainer)
}
/** Denitializer. This should never be called unless you know
what you are doing.
*/
deinit {
removeNotificationHandlers()
}
/// Overriding the layout callback for subviews.
public override func layoutSubviews() {
super.layoutSubviews()
placeholderLabel?.preferredMaxLayoutWidth = textContainer.size.width - textContainer.lineFragmentPadding * 2
titleLabel?.frame.size.width = bounds.width
public init() {
super.init(frame: CGRectNull)
}
/// Overriding the layout callback for sublayers.
public override func layoutSublayersOfLayer(layer: CALayer) {
super.layoutSublayersOfLayer(layer)
if self.layer == layer {
layoutShadowPath()
}
}
/**
A method that accepts CAAnimation objects and executes them on the
view's backing layer.
- Parameter animation: A CAAnimation instance.
*/
public func animate(animation: CAAnimation) {
animation.delegate = self
if let a: CABasicAnimation = animation as? CABasicAnimation {
a.fromValue = (nil == layer.presentationLayer() ? layer : layer.presentationLayer() as! CALayer).valueForKeyPath(a.keyPath!)
}
if let a: CAPropertyAnimation = animation as? CAPropertyAnimation {
layer.addAnimation(a, forKey: a.keyPath!)
} else if let a: CAAnimationGroup = animation as? CAAnimationGroup {
layer.addAnimation(a, forKey: nil)
} else if let a: CATransition = animation as? CATransition {
layer.addAnimation(a, forKey: kCATransition)
}
}
/**
A delegation method that is executed when the backing layer starts
running an animation.
- Parameter anim: The currently running CAAnimation instance.
*/
public override func animationDidStart(anim: CAAnimation) {
(delegate as? MaterialAnimationDelegate)?.materialAnimationDidStart?(anim)
}
/**
A delegation method that is executed when the backing layer stops
running an animation.
- Parameter anim: The CAAnimation instance that stopped running.
- Parameter flag: A boolean that indicates if the animation stopped
because it was completed or interrupted. True if completed, false
if interrupted.
*/
public override func animationDidStop(anim: CAAnimation, finished flag: Bool) {
if let a: CAPropertyAnimation = anim as? CAPropertyAnimation {
if let b: CABasicAnimation = a as? CABasicAnimation {
if let v: AnyObject = b.toValue {
if let k: String = b.keyPath {
layer.setValue(v, forKeyPath: k)
layer.removeAnimationForKey(k)
}
}
}
(delegate as? MaterialAnimationDelegate)?.materialAnimationDidStop?(anim, finished: flag)
} else if let a: CAAnimationGroup = anim as? CAAnimationGroup {
for x in a.animations! {
animationDidStop(x, finished: true)
}
}
}
/// Reloads necessary components when the view has changed.
internal func reloadView() {
if let p = placeholderLabel {
removeConstraints(constraints)
MaterialLayout.alignToParent(self,
child: p,
top: textContainerInset.top,
left: textContainerInset.left + textContainer.lineFragmentPadding,
bottom: textContainerInset.bottom,
right: textContainerInset.right + textContainer.lineFragmentPadding)
}
}
/// Notification handler for when text editing began.
internal func handleTextViewTextDidBegin() {
titleLabel?.textColor = titleLabelActiveColor
}
/// Notification handler for when text changed.
internal func handleTextViewTextDidChange() {
if let p = placeholderLabel {
p.hidden = !(true == text?.isEmpty)
}
if 0 < text?.utf16.count {
showTitleLabel()
} else if 0 == text?.utf16.count {
hideTitleLabel()
}
}
/// Notification handler for when text editing ended.
internal func handleTextViewTextDidEnd() {
if 0 < text?.utf16.count {
showTitleLabel()
} else if 0 == text?.utf16.count {
hideTitleLabel()
}
titleLabel?.textColor = titleLabelColor
}
/// Sets the shadow path.
internal func layoutShadowPath() {
if shadowPathAutoSizeEnabled {
if .None == depth {
shadowPath = nil
} else if nil == shadowPath {
shadowPath = UIBezierPath(roundedRect: bounds, cornerRadius: cornerRadius).CGPath
} else {
animate(MaterialAnimation.shadowPath(UIBezierPath(roundedRect: bounds, cornerRadius: cornerRadius).CGPath, duration: 0))
}
layoutLineLayer()
}
}
......@@ -467,85 +160,50 @@ public class TextView: UITextView {
The super.prepareView method should always be called immediately
when subclassing.
*/
private func prepareView() {
textContainerInset = MaterialEdgeInsetToValue(.None)
public override func prepareView() {
super.prepareView()
backgroundColor = MaterialColor.white
masksToBounds = false
removeNotificationHandlers()
prepareNotificationHandlers()
reloadView()
prepareTextView()
prepareTitleLabel()
prepareLineLayer()
}
/// prepares the placeholderLabel property.
private func preparePlaceholderLabel() {
if let v: UILabel = placeholderLabel {
v.translatesAutoresizingMaskIntoConstraints = false
v.font = font
v.textAlignment = textAlignment
v.numberOfLines = 0
v.backgroundColor = MaterialColor.clear
addSubview(v)
reloadView()
handleTextViewTextDidChange()
}
/// Prepares the textView.
private func prepareTextView() {
textView = MaterialTextView()
addSubview(textView)
textView.translatesAutoresizingMaskIntoConstraints = false
MaterialLayout.alignToParent(self, child: textView)
}
/// Prepares the titleLabel property.
/// Prepares the titleLabel.
private func prepareTitleLabel() {
if let v: UILabel = titleLabel {
v.hidden = true
addSubview(v)
if 0 < text?.utf16.count {
showTitleLabel()
} else {
v.alpha = 0
}
}
}
/// Shows and animates the titleLabel property.
private func showTitleLabel() {
if let v: UILabel = titleLabel {
if v.hidden {
if let s: String = placeholderLabel?.text {
v.text = s
}
let h: CGFloat = ceil(v.font.lineHeight)
v.frame = CGRectMake(0, -h, bounds.width, h)
v.hidden = false
UIView.animateWithDuration(0.25, animations: { [unowned self] in
v.alpha = 1
v.frame.origin.y = -v.frame.height - self.titleLabelAnimationDistance
})
}
}
}
/// Hides and animates the titleLabel property.
private func hideTitleLabel() {
if let v: UILabel = titleLabel {
if !v.hidden {
UIView.animateWithDuration(0.25, animations: {
v.alpha = 0
v.frame.origin.y = -v.frame.height
}) { _ in
v.hidden = true
}
}
}
titleLabel = UILabel()
titleLabel.hidden = true
titleLabel.font = RobotoFont.mediumWithSize(12)
addSubview(titleLabel)
titleLabelColor = placeholderTextColor
titleLabelActiveColor = MaterialColor.blue.accent3
// if 0 < text?.utf16.count {
//// showTitleLabel()
// } else {
// titleLabel.alpha = 0
// }
}
/// Prepares the Notification handlers.
private func prepareNotificationHandlers() {
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(handleTextViewTextDidBegin), name: UITextViewTextDidBeginEditingNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(handleTextViewTextDidChange), name: UITextViewTextDidChangeNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(handleTextViewTextDidEnd), name: UITextViewTextDidEndEditingNotification, object: nil)
/// Prepares the lineLayer.
private func prepareLineLayer() {
layoutLineLayer()
layer.addSublayer(lineLayer)
}
/// Removes the Notification handlers.
private func removeNotificationHandlers() {
NSNotificationCenter.defaultCenter().removeObserver(self, name: UITextViewTextDidBeginEditingNotification, object: nil)
NSNotificationCenter.defaultCenter().removeObserver(self, name: UITextViewTextDidChangeNotification, object: nil)
NSNotificationCenter.defaultCenter().removeObserver(self, name: UITextViewTextDidEndEditingNotification, object: nil)
/// Layout the lineLayer.
private func layoutLineLayer() {
let h: CGFloat = 1 < lineLayer.frame.height ? lineLayer.frame.height : 1
lineLayer.frame = CGRectMake(0, bounds.height + lineLayerDistance, bounds.width, h)
print(lineLayer.frame)
}
}
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