Updating Link class. adding identifier to molecule mapping.

This commit is contained in:
Kevin G Christiano 2020-01-13 13:31:25 -05:00
parent 9882528b9e
commit 5ce8446be2
2 changed files with 43 additions and 23 deletions

View File

@ -14,6 +14,11 @@ import UIKit
//-------------------------------------------------- //--------------------------------------------------
private var additionalData: [AnyHashable: Any]? private var additionalData: [AnyHashable: Any]?
//--------------------------------------------------
// MARK: - Delegate
//--------------------------------------------------
private var delegateObject: MVMCoreUIDelegateObject? private var delegateObject: MVMCoreUIDelegateObject?
//-------------------------------------------------- //--------------------------------------------------
@ -25,6 +30,10 @@ import UIKit
setupView() setupView()
} }
public convenience init() {
self.init(frame: .zero)
}
public required init?(coder: NSCoder) { public required init?(coder: NSCoder) {
super.init(coder: coder) super.init(coder: coder)
setupView() setupView()
@ -35,69 +44,77 @@ import UIKit
//-------------------------------------------------- //--------------------------------------------------
open override func draw(_ rect: CGRect) { open override func draw(_ rect: CGRect) {
guard let textRect = titleLabel?.frame else { return } 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 { 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 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 let y = textRect.origin.y + textRect.size.height + 1
contextRef?.move(to: CGPoint(x: x, y: y)) context?.move(to: CGPoint(x: x, y: y))
contextRef?.addLine(to: CGPoint(x: x + textRect.size.width, y: y)) context?.addLine(to: CGPoint(x: x + textRect.size.width, y: y))
contextRef?.closePath() context?.closePath()
contextRef?.drawPath(using: .stroke) context?.drawPath(using: .stroke)
} }
//-------------------------------------------------- //--------------------------------------------------
// MARK: - UITouch // 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<UITouch>, with event: UIEvent?) { open override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
MVMCoreActionHandler.shared()?.handleAction(with: actionMap, additionalData: additionalData, delegateObject: delegateObject) 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 // MARK: - MVMCoreViewProtocol
extension Link: MVMCoreViewProtocol { extension Link: MVMCoreViewProtocol {
public func updateView(_ size: CGFloat) { public func updateView(_ size: CGFloat) {
MVMCoreDispatchUtility.performBlock(onMainThread: { [weak self] in
DispatchQueue.main.async { [weak self] in
guard let self = self else { return } guard let self = self else { return }
var width = size var width = size
if MVMCoreGetterUtility.fequal(a: Float(CGFloat.leastNormalMagnitude), b: Float(size)) { if MVMCoreGetterUtility.fequal(a: Float(CGFloat.leastNormalMagnitude), b: Float(size)) {
width = MVMCoreUIUtility.getWidth() width = MVMCoreUIUtility.getWidth()
} }
self.titleLabel?.font = MFStyler.fontB2(forWidth: width) self.titleLabel?.font = MFStyler.fontB2(forWidth: width)
}) }
} }
public func setupView() { public func setupView() {
translatesAutoresizingMaskIntoConstraints = false translatesAutoresizingMaskIntoConstraints = false
backgroundColor = .clear backgroundColor = .clear
contentMode = .redraw contentMode = .redraw
setTitleColor(.mfTextButton(), for: .normal) setTitleColor(.mfTextButton(), for: .normal)
setTitleColor(.mfCharcoal(), for: .highlighted) setTitleColor(.mfCharcoal(), for: .highlighted)
// left alignment by default
titleLabel?.textAlignment = .left titleLabel?.textAlignment = .left
contentHorizontalAlignment = .left contentHorizontalAlignment = .left
titleLabel?.numberOfLines = 1
} }
} }
// MARK: - MVMCoreUIMoleculeViewProtocol
extension Link: MVMCoreUIMoleculeViewProtocol { extension Link: MVMCoreUIMoleculeViewProtocol {
public func reset() { public func reset() {
@ -105,17 +122,18 @@ extension Link: MVMCoreUIMoleculeViewProtocol {
} }
public func setWithJSON(_ json: [AnyHashable: Any]?, delegateObject: MVMCoreUIDelegateObject?, additionalData: [AnyHashable: Any]?) { public func setWithJSON(_ json: [AnyHashable: Any]?, delegateObject: MVMCoreUIDelegateObject?, additionalData: [AnyHashable: Any]?) {
guard let unwrappedJson = json else { return }
actionMap = unwrappedJson
self.additionalData = additionalData self.additionalData = additionalData
self.delegateObject = delegateObject self.delegateObject = delegateObject
guard let unwrappedJson = json else { return }
actionMap = unwrappedJson
buttonDelegate = delegateObject?.buttonDelegate buttonDelegate = delegateObject?.buttonDelegate
let color = unwrappedJson.stringForkey(KeyTextColor) let color = unwrappedJson.stringForkey(KeyTextColor)
setTitleColor(.mfGet(forHex: color), for: .normal) setTitleColor(.mfGet(forHex: color), for: .normal)
titleLabel?.numberOfLines = 0
titleLabel?.lineBreakMode = .byWordWrapping;
if let title = unwrappedJson.optionalStringForKey(KeyTitle) { if let title = unwrappedJson.optionalStringForKey(KeyTitle) {
setTitle(title, for: .normal) setTitle(title, for: .normal)
} }
@ -130,6 +148,7 @@ extension Link: MVMCoreUIMoleculeViewProtocol {
} }
} }
// MARK:- MVMCoreUIViewConstrainingProtocol
extension Link: MVMCoreUIViewConstrainingProtocol { extension Link: MVMCoreUIViewConstrainingProtocol {
public func needsToBeConstrained() -> Bool { public func needsToBeConstrained() -> Bool {

View File

@ -27,6 +27,7 @@
mapping = [@{ mapping = [@{
@"label": Label.class, @"label": Label.class,
@"line": Line.class, @"line": Line.class,
@"link": Link.class,
@"button": PrimaryButton.class, @"button": PrimaryButton.class,
@"textButton": MFTextButton.class, @"textButton": MFTextButton.class,
@"header": StandardHeaderView.class, @"header": StandardHeaderView.class,