Make Link subclass from View instead of MFCustomButton
The bounds of the Link often extend far beyond the width of the button text, which, if MFCustomButton is used, extend the tappable area far beyond the link.
This commit is contained in:
parent
d3dbfdd997
commit
439261e4c4
@ -8,7 +8,7 @@
|
|||||||
|
|
||||||
import UIKit
|
import UIKit
|
||||||
|
|
||||||
@objcMembers open class Link: MFCustomButton {
|
@objcMembers open class Link: View {
|
||||||
|
|
||||||
private lazy var sizeObject: MFSizeObject? = {
|
private lazy var sizeObject: MFSizeObject? = {
|
||||||
return MFSizeObject(standardSize: 30, standardiPadPortraitSize: 36, iPadProLandscapeSize: 42)
|
return MFSizeObject(standardSize: 30, standardiPadPortraitSize: 36, iPadProLandscapeSize: 42)
|
||||||
@ -16,6 +16,7 @@ import UIKit
|
|||||||
|
|
||||||
private var additionalData: [AnyHashable: Any]?
|
private var additionalData: [AnyHashable: Any]?
|
||||||
private var delegateObject: MVMCoreUIDelegateObject?
|
private var delegateObject: MVMCoreUIDelegateObject?
|
||||||
|
public let linkButton = MFCustomButton()
|
||||||
|
|
||||||
public override init(frame: CGRect) {
|
public override init(frame: CGRect) {
|
||||||
super.init(frame: frame)
|
super.init(frame: frame)
|
||||||
@ -28,12 +29,12 @@ import UIKit
|
|||||||
}
|
}
|
||||||
|
|
||||||
open override func draw(_ rect: CGRect) {
|
open override func draw(_ rect: CGRect) {
|
||||||
guard let textRect = self.titleLabel?.frame else { return }
|
guard let textRect = self.linkButton.titleLabel?.frame else { return }
|
||||||
|
|
||||||
let contextRef = UIGraphicsGetCurrentContext()
|
let contextRef = UIGraphicsGetCurrentContext()
|
||||||
|
|
||||||
//set to the same color as the text
|
//set to the same color as the text
|
||||||
if let color = self.titleLabel?.textColor?.cgColor {
|
if let color = self.linkButton.titleLabel?.textColor?.cgColor {
|
||||||
contextRef?.setStrokeColor(color)
|
contextRef?.setStrokeColor(color)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -49,86 +50,97 @@ import UIKit
|
|||||||
}
|
}
|
||||||
|
|
||||||
override open func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
|
override open func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
|
||||||
|
|
||||||
let faultTolerance: CGFloat = 20.0
|
let faultTolerance: CGFloat = 20.0
|
||||||
let area = bounds.insetBy(dx: -faultTolerance, dy: -faultTolerance)
|
let area = linkButton.bounds.insetBy(dx: -faultTolerance, dy: -faultTolerance)
|
||||||
return area.contains(point)
|
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)
|
if linkButton.isEnabled {
|
||||||
|
MVMCoreActionHandler.shared()?.handleAction(with: linkButton.actionMap, additionalData: additionalData, delegateObject: delegateObject)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
extension Link: MVMCoreViewProtocol {
|
//MARK: MVMCoreViewProtocol
|
||||||
|
extension Link {
|
||||||
|
|
||||||
public func updateView(_ size: CGFloat) {
|
public override func updateView(_ size: CGFloat) {
|
||||||
MVMCoreDispatchUtility.performBlock(onMainThread: { [weak self] in
|
MVMCoreDispatchUtility.performBlock(onMainThread: { [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 = MVMCoreUISplitViewController.getApplicationViewWidth()
|
width = MVMCoreUISplitViewController.getApplicationViewWidth()
|
||||||
}
|
}
|
||||||
self.titleLabel?.font = MFStyler.fontB2(forWidth: width)
|
self.linkButton.titleLabel?.font = MFStyler.fontB2(forWidth: width)
|
||||||
guard let heightConstraint = self.heightConstraint else { return }
|
guard let heightConstraint = self.linkButton.heightConstraint else { return }
|
||||||
if !MVMCoreGetterUtility.fequal(a: 0, b: Float(heightConstraint.constant)) {
|
if !MVMCoreGetterUtility.fequal(a: 0, b: Float(heightConstraint.constant)) {
|
||||||
guard let sizeObject = self.sizeObject else { return }
|
guard let sizeObject = self.sizeObject else { return }
|
||||||
self.heightConstraint?.constant = sizeObject.getValueBased(onSize: width)
|
self.linkButton.heightConstraint?.constant = sizeObject.getValueBased(onSize: width)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
public func setupView() {
|
public override func setupView() {
|
||||||
|
linkButton.isUserInteractionEnabled = false
|
||||||
|
linkButton.translatesAutoresizingMaskIntoConstraints = false
|
||||||
|
addSubview(linkButton)
|
||||||
|
NSLayoutConstraint.activate([
|
||||||
|
linkButton.leadingAnchor.constraint(equalTo: leadingAnchor),
|
||||||
|
linkButton.topAnchor.constraint(equalTo: topAnchor),
|
||||||
|
trailingAnchor.constraint(greaterThanOrEqualTo: linkButton.trailingAnchor),
|
||||||
|
bottomAnchor.constraint(equalTo: linkButton.bottomAnchor)
|
||||||
|
])
|
||||||
translatesAutoresizingMaskIntoConstraints = false
|
translatesAutoresizingMaskIntoConstraints = false
|
||||||
backgroundColor = .clear
|
backgroundColor = .clear
|
||||||
contentMode = .redraw
|
contentMode = .redraw
|
||||||
setTitleColor(.mfTextButton(), for: .normal)
|
linkButton.setTitleColor(.mfTextButton(), for: .normal)
|
||||||
setTitleColor(.mfCharcoal(), for: .highlighted)
|
linkButton.setTitleColor(.mfCharcoal(), for: .highlighted)
|
||||||
// left alignment by default
|
linkButton.titleLabel?.textAlignment = .left
|
||||||
titleLabel?.textAlignment = .left
|
linkButton.contentHorizontalAlignment = .left
|
||||||
contentHorizontalAlignment = .left
|
|
||||||
|
|
||||||
if let constant = self.sizeObject?.standardSize {
|
if let constant = self.sizeObject?.standardSize {
|
||||||
let heightConstraint = NSLayoutConstraint(item: self as Any, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: constant)
|
let heightConstraint = NSLayoutConstraint(item: self as Any, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: constant)
|
||||||
addConstraint(heightConstraint)
|
addConstraint(heightConstraint)
|
||||||
heightConstraint.isActive = true
|
heightConstraint.isActive = true
|
||||||
self.heightConstraint = heightConstraint
|
self.linkButton.heightConstraint = heightConstraint
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
extension Link: MVMCoreUIMoleculeViewProtocol {
|
//MARK: MVMCoreUIMoleculeViewProtocol
|
||||||
|
extension Link {
|
||||||
|
|
||||||
public func reset() {
|
public override func reset() {
|
||||||
self.setTitleColor(.mfTextButton(), for: .normal)
|
self.linkButton.setTitleColor(.mfTextButton(), for: .normal)
|
||||||
|
self.linkButton.isEnabled = true
|
||||||
}
|
}
|
||||||
|
|
||||||
public func setWithJSON(_ json: [AnyHashable: Any]?, delegateObject: MVMCoreUIDelegateObject?, additionalData: [AnyHashable: Any]?) {
|
public override func setWithJSON(_ json: [AnyHashable: Any]?, delegateObject: MVMCoreUIDelegateObject?, additionalData: [AnyHashable: Any]?) {
|
||||||
if let unwrappedJson = json {
|
if let unwrappedJson = json {
|
||||||
actionMap = unwrappedJson
|
linkButton.actionMap = unwrappedJson
|
||||||
self.additionalData = additionalData
|
self.additionalData = additionalData
|
||||||
self.delegateObject = delegateObject
|
self.delegateObject = delegateObject
|
||||||
buttonDelegate = delegateObject?.buttonDelegate
|
linkButton.buttonDelegate = delegateObject?.buttonDelegate
|
||||||
|
|
||||||
let color = unwrappedJson.stringForkey(KeyTextColor)
|
let color = unwrappedJson.stringForkey(KeyTextColor)
|
||||||
setTitleColor(.mfGet(forHex: color), for: .normal)
|
linkButton.setTitleColor(.mfGet(forHex: color), for: .normal)
|
||||||
|
|
||||||
titleLabel?.numberOfLines = 0
|
linkButton.titleLabel?.numberOfLines = 0
|
||||||
titleLabel?.lineBreakMode = .byWordWrapping;
|
linkButton.titleLabel?.lineBreakMode = .byWordWrapping;
|
||||||
setTitle(actionMap?.stringForkey(KeyTitle), for: .normal)
|
linkButton.setTitle(linkButton.actionMap?.stringForkey(KeyTitle), for: .normal)
|
||||||
|
|
||||||
if let enabled = unwrappedJson[KeyEnabled] as? Bool {
|
if let enabled = unwrappedJson[KeyEnabled] as? Bool {
|
||||||
isEnabled = enabled
|
linkButton.isEnabled = enabled
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if let title = self.title(for: .normal),
|
if let title = self.linkButton.title(for: .normal),
|
||||||
title.isEmpty {
|
title.isEmpty {
|
||||||
self.heightConstraint?.constant = 0
|
self.linkButton.heightConstraint?.constant = 0
|
||||||
} else {
|
} else {
|
||||||
guard let standardSize = sizeObject?.standardSize else { return }
|
guard let standardSize = sizeObject?.standardSize else { return }
|
||||||
heightConstraint?.constant = standardSize
|
linkButton.heightConstraint?.constant = standardSize
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user