diff --git a/MVMCoreUI/Atoms/Buttons/CaretButton.swift b/MVMCoreUI/Atoms/Buttons/CaretButton.swift index 8f40568c..1bee754f 100644 --- a/MVMCoreUI/Atoms/Buttons/CaretButton.swift +++ b/MVMCoreUI/Atoms/Buttons/CaretButton.swift @@ -9,7 +9,6 @@ open class CaretButton: MFCustomButton, MVMCoreUIMoleculeViewProtocol, MVMCoreUIViewConstrainingProtocol { - //------------------------------------------------------ // MARK: - Constants //------------------------------------------------------ @@ -54,15 +53,16 @@ open class CaretButton: MFCustomButton, MVMCoreUIMoleculeViewProtocol, MVMCoreUI public func updateView(_ size: CGFloat) { } //------------------------------------------------------ - // MARK: - Functions + // MARK: - Methods //------------------------------------------------------ private func changeCaretColor() { + setTitleColor(enabledColor, for: .normal) setTitleColor(disabledColor, for: .disabled) if let rightCaretView = rightView as? CaretView { - rightCaretView.setLineColor(isEnabled ? enabledColor : disabledColor) + rightCaretView.isEnabled = isEnabled } } @@ -81,8 +81,8 @@ open class CaretButton: MFCustomButton, MVMCoreUIMoleculeViewProtocol, MVMCoreUI rightView?.translatesAutoresizingMaskIntoConstraints = false addSubview(caretView) - NSLayoutConstraint(item: caretView, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: width).isActive = true - NSLayoutConstraint(item: caretView, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: height).isActive = true + caretView.widthAnchor.constraint(equalToConstant: width).isActive = true + caretView.heightAnchor.constraint(equalToConstant: height).isActive = true let caretLabelSpacing = NSLayoutConstraint(item: caretView, attribute: .left, relatedBy: .equal, toItem: titleLabel, attribute: .right, multiplier: 1.0, constant: 4.0) caretLabelSpacing.isActive = true @@ -91,6 +91,7 @@ open class CaretButton: MFCustomButton, MVMCoreUIMoleculeViewProtocol, MVMCoreUI NSLayoutConstraint(item: caretView, attribute: .centerY, relatedBy: .equal, toItem: self, attribute: .centerY, multiplier: 1.0, constant: 0).isActive = true NSLayoutConstraint(item: self, attribute: .right, relatedBy: .greaterThanOrEqual, toItem: caretView, attribute: .right, multiplier: 1.0, constant: 0).isActive = true contentHorizontalAlignment = .left + //set correct color after layout changeCaretColor() } @@ -135,10 +136,10 @@ open class CaretButton: MFCustomButton, MVMCoreUIMoleculeViewProtocol, MVMCoreUI } open func alignment() -> UIStackView.Alignment { - return UIStackView.Alignment.leading; + return .leading } - public class func estimatedHeight(forRow json: [AnyHashable : Any]?, delegateObject: MVMCoreUIDelegateObject?) -> CGFloat { + public class func estimatedHeight(forRow json: [AnyHashable: Any]?, delegateObject: MVMCoreUIDelegateObject?) -> CGFloat { return 10 } } diff --git a/MVMCoreUI/Atoms/TextFields/DropdownEntryField.swift b/MVMCoreUI/Atoms/TextFields/DropdownEntryField.swift index 17627b69..a553913c 100644 --- a/MVMCoreUI/Atoms/TextFields/DropdownEntryField.swift +++ b/MVMCoreUI/Atoms/TextFields/DropdownEntryField.swift @@ -71,6 +71,10 @@ import UIKit fatalError("DropdownEntryField does not support xib.") } + //-------------------------------------------------- + // MARK: - Setup + //-------------------------------------------------- + private func setup() { dropDownCaretLabel.heightAnchor.constraint(equalToConstant: 40).isActive = true @@ -84,13 +88,15 @@ import UIKit open override func setupFieldContainerContent(_ container: UIView) { - let tapOnCarrot = UITapGestureRecognizer(target: self, action: #selector(startEditing)) - dropDownCaretLabel.addGestureRecognizer(tapOnCarrot) + let tapOnCaret = UITapGestureRecognizer(target: self, action: #selector(startEditing)) + dropDownCaretLabel.addGestureRecognizer(tapOnCaret) container.addSubview(dropDownCaretLabel) + textFieldTrailingConstraint?.isActive = false dropDownCaretLabel.topAnchor.constraint(equalTo: container.topAnchor).isActive = true - dropDownCaretLabel.leadingAnchor.constraint(equalTo: textField.trailingAnchor, constant: 6).isActive = true + textFieldTrailingConstraint = dropDownCaretLabel.leadingAnchor.constraint(equalTo: textField.trailingAnchor, constant: 6) + textFieldTrailingConstraint?.isActive = true container.trailingAnchor.constraint(equalTo: dropDownCaretLabel.trailingAnchor, constant: 16).isActive = true container.bottomAnchor.constraint(equalTo: dropDownCaretLabel.bottomAnchor).isActive = true dropDownCaretWidth = dropDownCaretLabel.widthAnchor.constraint(equalToConstant: 0) diff --git a/MVMCoreUI/Atoms/TextFields/FormEntryField.swift b/MVMCoreUI/Atoms/TextFields/FormEntryField.swift index 40157483..b0638253 100644 --- a/MVMCoreUI/Atoms/TextFields/FormEntryField.swift +++ b/MVMCoreUI/Atoms/TextFields/FormEntryField.swift @@ -67,7 +67,6 @@ import UIKit layer.backgroundColor = UIColor.black.cgColor layer.drawsAsynchronously = true layer.anchorPoint = CGPoint(x: 0.5, y: 1.0); - return layer }() diff --git a/MVMCoreUI/Atoms/TextFields/TextEntryField.swift b/MVMCoreUI/Atoms/TextFields/TextEntryField.swift index 53807ec5..9b0bfbb2 100644 --- a/MVMCoreUI/Atoms/TextFields/TextEntryField.swift +++ b/MVMCoreUI/Atoms/TextFields/TextEntryField.swift @@ -145,9 +145,9 @@ import UIKit NSLayoutConstraint.activate([ textField.heightAnchor.constraint(equalToConstant: 24), - textField.topAnchor.constraint(equalTo: container.topAnchor, constant: 10), + textField.topAnchor.constraint(equalTo: container.topAnchor, constant: 12), textField.leadingAnchor.constraint(equalTo: container.leadingAnchor, constant: 16), - container.bottomAnchor.constraint(equalTo: textField.bottomAnchor, constant: 10)]) + container.bottomAnchor.constraint(equalTo: textField.bottomAnchor, constant: 12)]) textFieldTrailingConstraint = container.trailingAnchor.constraint(equalTo: textField.trailingAnchor, constant: 16) textFieldTrailingConstraint?.isActive = true diff --git a/MVMCoreUI/Atoms/Views/CaretView.swift b/MVMCoreUI/Atoms/Views/CaretView.swift index 4f1f80eb..519965a1 100644 --- a/MVMCoreUI/Atoms/Views/CaretView.swift +++ b/MVMCoreUI/Atoms/Views/CaretView.swift @@ -12,17 +12,51 @@ // MARK: - Properties //------------------------------------------------------ - // Objc can't use float enum. - public static let thin: CGFloat = 6.0 - public static let standard: CGFloat = 2.5 - public static let thick: CGFloat = 1.5 - public var strokeColor: UIColor = .black public var lineWidth: CGFloat = 1 public var direction: Direction = .right + public var size: CaretSize? private var caretPath: UIBezierPath = UIBezierPath() + public var enabledColor: UIColor = .black + public var disabledColor: UIColor = .mfSilver() + + public var isEnabled: Bool = true { + didSet { + strokeColor = isEnabled ? enabledColor : disabledColor + setNeedsDisplay() + } + } + + //------------------------------------------------------ + // MARK: - Constraints + //------------------------------------------------------ + + public enum CaretSize: String { + case small + case medium + case large + + // Dimensions of container; provided by InVision. + func dimensions() -> CGSize { + + switch self { + case .small: + return CGSize(width: 6, height: 10) + + case .medium: + return CGSize(width: 9, height: 16) + + case .large: + return CGSize(width: 14, height: 24) + } + } + } + + public var heightConstraint: NSLayoutConstraint? + public var widthConstraint: NSLayoutConstraint? + //------------------------------------------------------ // MARK: - Initialization //------------------------------------------------------ @@ -31,28 +65,31 @@ super.init(frame: frame) } - @objc public init() { - super.init(frame: .zero) + @objc public convenience init() { + self.init(frame: .zero) } - @objc public init(lineWidth: CGFloat) { - super.init(frame: .zero) + @objc public convenience init(lineWidth: CGFloat) { + self.init(frame: .zero) self.lineWidth = lineWidth } - /// Can init with a specific line thickness, scales based on width and height. - @objc public init(lineThickness: CGFloat) { - super.init(frame: .zero) - // self.lineThickness = lineThickness - } - @objc required public init?(coder aDecoder: NSCoder) { super.init(coder: aDecoder) fatalError("CaretView xib not supported.") } + //------------------------------------------------------ + // MARK: - Setup + //------------------------------------------------------ + @objc override open func setupView() { + defaultState() + + // Establishing references. + heightConstraint = heightAnchor.constraint(equalToConstant: 0) + widthConstraint = widthAnchor.constraint(equalToConstant: 0) } //------------------------------------------------------ @@ -60,7 +97,7 @@ //------------------------------------------------------ /// The direction the caret will be pointing to. - public enum Direction: String { + @objc public enum Direction: Int { case left case right case down @@ -73,27 +110,30 @@ caretPath.removeAllPoints() caretPath.lineJoinStyle = .miter caretPath.lineWidth = lineWidth + let inset = lineWidth / 2 + let halfWidth = frame.size.width / 2 + let halfHeight = frame.size.height / 2 switch direction { case .up: caretPath.move(to: CGPoint(x: inset, y: frame.size.height - inset)) - caretPath.addLine(to: CGPoint(x: frame.size.width / 2, y: inset)) + caretPath.addLine(to: CGPoint(x: halfWidth, y: lineWidth)) caretPath.addLine(to: CGPoint(x: frame.size.width, y: frame.size.height)) case .right: caretPath.move(to: CGPoint(x: inset, y: inset)) - caretPath.addLine(to: CGPoint(x: frame.size.width - inset, y: frame.size.height / 2)) + caretPath.addLine(to: CGPoint(x: frame.size.width - lineWidth, y: halfHeight)) caretPath.addLine(to: CGPoint(x: inset, y: frame.size.height - inset)) case .down: caretPath.move(to: CGPoint(x: inset, y: inset)) - caretPath.addLine(to: CGPoint(x: frame.size.width / 2, y: frame.size.height - inset)) + caretPath.addLine(to: CGPoint(x: halfWidth, y: frame.size.height - lineWidth)) caretPath.addLine(to: CGPoint(x: frame.size.width - inset, y: inset)) case .left: caretPath.move(to: CGPoint(x: frame.size.width - inset, y: inset)) - caretPath.addLine(to: CGPoint(x: inset, y: frame.size.height / 2)) + caretPath.addLine(to: CGPoint(x: lineWidth, y: halfHeight)) caretPath.addLine(to: CGPoint(x: frame.size.width - inset, y: frame.size.height - inset)) } @@ -106,17 +146,29 @@ //------------------------------------------------------ @objc public func setLineColor(_ color: UIColor) { + strokeColor = color setNeedsDisplay() } - private func defaultState() { + @objc public func defaultState() { + + translatesAutoresizingMaskIntoConstraints = false isOpaque = false isHidden = false backgroundColor = .clear strokeColor = .black } + @objc public func setConstraints() { + + guard let dimensions = size?.dimensions() else { return } + + heightConstraint?.constant = dimensions.height + heightConstraint?.isActive = true + widthConstraint?.constant = dimensions.width + widthConstraint?.isActive = true + } //------------------------------------------------------ // MARK: - Atomization @@ -127,7 +179,7 @@ defaultState() } - open override func setWithJSON(_ json: [AnyHashable: Any]?, delegateObject: MVMCoreUIDelegateObject?, additionalData: [AnyHashable: Any]?) { + @objc open override func setWithJSON(_ json: [AnyHashable: Any]?, delegateObject: MVMCoreUIDelegateObject?, additionalData: [AnyHashable: Any]?) { super.setWithJSON(json, delegateObject: delegateObject, additionalData: additionalData) guard let dictionary = json else { return } @@ -149,11 +201,11 @@ } } - open override func needsToBeConstrained() -> Bool { + @objc open override func needsToBeConstrained() -> Bool { return true } - open override func alignment() -> UIStackView.Alignment { + @objc open override func alignment() -> UIStackView.Alignment { return .leading } } diff --git a/MVMCoreUI/Atoms/Views/DashLine.swift b/MVMCoreUI/Atoms/Views/DashLine.swift index 2bc11b98..bcac274b 100644 --- a/MVMCoreUI/Atoms/Views/DashLine.swift +++ b/MVMCoreUI/Atoms/Views/DashLine.swift @@ -51,12 +51,7 @@ open class DashLine: MFView { dashLayer.backgroundColor = UIColor.clear.cgColor dashLayer.frame = bounds - if let sublayers = layer.sublayers { - for sublayer in sublayers { - sublayer.removeFromSuperlayer() - } - } - + layer.sublayers?.forEach { $0.removeFromSuperlayer() } layer.addSublayer(dashLayer) let path = UIBezierPath() diff --git a/MVMCoreUI/Molecules/Items/TableViewCell.swift b/MVMCoreUI/Molecules/Items/TableViewCell.swift index cc531d75..3dd85f71 100644 --- a/MVMCoreUI/Molecules/Items/TableViewCell.swift +++ b/MVMCoreUI/Molecules/Items/TableViewCell.swift @@ -204,13 +204,17 @@ import UIKit // MARK: - Arrow /// Adds the standard mvm style caret to the accessory view @objc public func addCaretViewAccessory() { + guard accessoryView == nil else { return } - let width: CGFloat = 6 - let height: CGFloat = 10 - caretView = CaretView(lineThickness: CaretView.thin) - caretView?.frame = CGRect(x: 0, y: 0, width: width, height: height) - caretViewWidthSizeObject = MFSizeObject(standardSize: width, standardiPadPortraitSize: 9) - caretViewHeightSizeObject = MFSizeObject(standardSize: height, standardiPadPortraitSize: 16) + + caretView = CaretView(lineWidth: 1) + caretView?.size = .small + caretView?.setConstraints() + + if let size = caretView?.size?.dimensions() { + caretViewWidthSizeObject = MFSizeObject(standardSize: size.width, standardiPadPortraitSize: 9) + caretViewHeightSizeObject = MFSizeObject(standardSize: size.height, standardiPadPortraitSize: 16) + } accessoryView = caretView }