From 5ce8446be2edbf2857fc56b8cbc4fe06dc47ab26 Mon Sep 17 00:00:00 2001 From: Kevin G Christiano Date: Mon, 13 Jan 2020 13:31:25 -0500 Subject: [PATCH] Updating Link class. adding identifier to molecule mapping. --- MVMCoreUI/Atoms/Buttons/Link.swift | 65 ++++++++++++------- .../MVMCoreUIMoleculeMappingObject.m | 1 + 2 files changed, 43 insertions(+), 23 deletions(-) diff --git a/MVMCoreUI/Atoms/Buttons/Link.swift b/MVMCoreUI/Atoms/Buttons/Link.swift index 9008c398..394807b6 100644 --- a/MVMCoreUI/Atoms/Buttons/Link.swift +++ b/MVMCoreUI/Atoms/Buttons/Link.swift @@ -14,6 +14,11 @@ import UIKit //-------------------------------------------------- private var additionalData: [AnyHashable: Any]? + + //-------------------------------------------------- + // MARK: - Delegate + //-------------------------------------------------- + private var delegateObject: MVMCoreUIDelegateObject? //-------------------------------------------------- @@ -25,6 +30,10 @@ import UIKit setupView() } + public convenience init() { + self.init(frame: .zero) + } + public required init?(coder: NSCoder) { super.init(coder: coder) setupView() @@ -35,69 +44,77 @@ import UIKit //-------------------------------------------------- open override func draw(_ rect: CGRect) { + guard let textRect = titleLabel?.frame else { return } - let contextRef = UIGraphicsGetCurrentContext() + let context = UIGraphicsGetCurrentContext() - //set to the same color as the text + // Set to the same color as the text if let color = titleLabel?.textColor?.cgColor { - contextRef?.setStrokeColor(color) + context?.setStrokeColor(color) } - //x should be according to the text, not the button + // x should be according to the text, not the button let x = textRect.origin.x - // line is 1 point below the text + + // Line is 1 point below the text let y = textRect.origin.y + textRect.size.height + 1 - contextRef?.move(to: CGPoint(x: x, y: y)) - contextRef?.addLine(to: CGPoint(x: x + textRect.size.width, y: y)) - contextRef?.closePath() - contextRef?.drawPath(using: .stroke) + context?.move(to: CGPoint(x: x, y: y)) + context?.addLine(to: CGPoint(x: x + textRect.size.width, y: y)) + context?.closePath() + context?.drawPath(using: .stroke) } //-------------------------------------------------- // MARK: - UITouch //-------------------------------------------------- - override open func point(inside point: CGPoint, with event: UIEvent?) -> Bool { - - let faultTolerance: CGFloat = 20.0 - let area = bounds.insetBy(dx: -faultTolerance, dy: -faultTolerance) - return area.contains(point) - } - open override func touchesEnded(_ touches: Set, with event: UIEvent?) { MVMCoreActionHandler.shared()?.handleAction(with: actionMap, additionalData: additionalData, delegateObject: delegateObject) } } +// MARK: - AppleGuidelinesProtocol +extension Link: AppleGuidelinesProtocol { + + override open func point(inside point: CGPoint, with event: UIEvent?) -> Bool { + + return Self.acceptablyOutsideBounds(point: point, bounds: bounds) + } +} + // MARK: - MVMCoreViewProtocol extension Link: MVMCoreViewProtocol { public func updateView(_ size: CGFloat) { - MVMCoreDispatchUtility.performBlock(onMainThread: { [weak self] in + + DispatchQueue.main.async { [weak self] in guard let self = self else { return } var width = size if MVMCoreGetterUtility.fequal(a: Float(CGFloat.leastNormalMagnitude), b: Float(size)) { width = MVMCoreUIUtility.getWidth() } + self.titleLabel?.font = MFStyler.fontB2(forWidth: width) - }) + } } public func setupView() { + translatesAutoresizingMaskIntoConstraints = false backgroundColor = .clear contentMode = .redraw setTitleColor(.mfTextButton(), for: .normal) setTitleColor(.mfCharcoal(), for: .highlighted) - // left alignment by default titleLabel?.textAlignment = .left contentHorizontalAlignment = .left + titleLabel?.numberOfLines = 1 } } +// MARK: - MVMCoreUIMoleculeViewProtocol extension Link: MVMCoreUIMoleculeViewProtocol { public func reset() { @@ -105,17 +122,18 @@ extension Link: MVMCoreUIMoleculeViewProtocol { } public func setWithJSON(_ json: [AnyHashable: Any]?, delegateObject: MVMCoreUIDelegateObject?, additionalData: [AnyHashable: Any]?) { - guard let unwrappedJson = json else { return } - actionMap = unwrappedJson self.additionalData = additionalData self.delegateObject = delegateObject + + guard let unwrappedJson = json else { return } + actionMap = unwrappedJson + buttonDelegate = delegateObject?.buttonDelegate let color = unwrappedJson.stringForkey(KeyTextColor) setTitleColor(.mfGet(forHex: color), for: .normal) - titleLabel?.numberOfLines = 0 - titleLabel?.lineBreakMode = .byWordWrapping; + if let title = unwrappedJson.optionalStringForKey(KeyTitle) { setTitle(title, for: .normal) } @@ -130,6 +148,7 @@ extension Link: MVMCoreUIMoleculeViewProtocol { } } +// MARK:- MVMCoreUIViewConstrainingProtocol extension Link: MVMCoreUIViewConstrainingProtocol { public func needsToBeConstrained() -> Bool { diff --git a/MVMCoreUI/OtherHandlers/MVMCoreUIMoleculeMappingObject.m b/MVMCoreUI/OtherHandlers/MVMCoreUIMoleculeMappingObject.m index ecad0fc7..b43852a0 100644 --- a/MVMCoreUI/OtherHandlers/MVMCoreUIMoleculeMappingObject.m +++ b/MVMCoreUI/OtherHandlers/MVMCoreUIMoleculeMappingObject.m @@ -27,6 +27,7 @@ mapping = [@{ @"label": Label.class, @"line": Line.class, + @"link": Link.class, @"button": PrimaryButton.class, @"textButton": MFTextButton.class, @"header": StandardHeaderView.class,