Latest changes. Now including clause changes.

This commit is contained in:
Kevin G Christiano 2019-09-05 11:20:55 -04:00
parent a6e732afa9
commit 33754802bb
2 changed files with 27 additions and 13 deletions

View File

@ -56,13 +56,22 @@ public typealias ActionBlock = () -> ()
public struct ActionableClause {
var range: NSRange?
var actionBlock: ActionBlock?
var hash: Int = 0
var accessibilityID: Int = 0
func performAction() {
actionBlock?()
}
}
//------------------------------------------------------
// MARK: - Convenience Setter For objective-C
//------------------------------------------------------
/// Sets the clauses array to empty.
@objc public func setEmptyClauses() {
clauses = []
}
//------------------------------------------------------
// MARK: - Initialization
//------------------------------------------------------
@ -73,6 +82,7 @@ public typealias ActionBlock = () -> ()
numberOfLines = 0
lineBreakMode = .byWordWrapping
translatesAutoresizingMaskIntoConstraints = false
clauses = []
accessibilityCustomActions = []
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(textLinkTapped(_:)))
@ -295,9 +305,8 @@ public typealias ActionBlock = () -> ()
guard let actionLabel = label as? Label else { continue }
actionLabel.addActionAttributes(range: range, string: attributedString)
let accessibleAction = actionLabel.customAccessibilityAction(range: range)
let actionBlock = actionLabel.createActionBlockFrom(actionMap: json, additionalData: additionalData, delegateObject: delegate)
actionLabel.clauses.append(ActionableClause(range: range, actionBlock: actionBlock, hash: accessibleAction?.hash ?? -1))
actionLabel.appendActionableClause(range: range, actionBlock: actionBlock)
default:
continue
@ -402,7 +411,7 @@ public typealias ActionBlock = () -> ()
/**
Appends an external link image to the end of the attributed string.
Will provide one whitespace to the left of the icon
Will provide one whitespace to the left of the icon; adds 2 chars to the end of the string.
*/
@objc public func appendExternalLinkIcon() {
@ -419,7 +428,7 @@ public typealias ActionBlock = () -> ()
Insert external link icon anywhere within text of Label.
- Note: Each icon insertion adds 1 additional characters to the overall text length.
Therefore, you MUST insert icons and links in the order they would appear.
Therefore, you **MUST** insert icons and links in the order they would appear.
- parameter index: Location within the associated text to insert an external Link Icon
*/
public func insertExternalLinkIcon(at index: Int) {
@ -487,6 +496,12 @@ public typealias ActionBlock = () -> ()
return containsAttachment
}
func appendActionableClause(range: NSRange, actionBlock: @escaping ActionBlock) {
let accessibleAction = customAccessibilityAction(range: range)
clauses.append(ActionableClause(range: range, actionBlock: actionBlock, accessibilityID: accessibleAction?.hash ?? -1))
}
}
// MARK: - Atomization
@ -499,6 +514,7 @@ extension Label {
originalAttributedString = nil
styleB2(true)
accessibilityCustomActions = []
clauses = []
}
@objc public func setWithJSON(_ json: [AnyHashable: Any]?, delegateObject: MVMCoreUIDelegateObject?, additionalData: [AnyHashable: Any]?) {
@ -527,7 +543,7 @@ extension Label {
// MARK: - Multi-Action Functionality
extension Label {
/// Reseting to default Label values.
/// Applied to existing text. Removes underlines of tappable links and assoated actionable clauses.
@objc public func clearActionableClauses() {
guard let attributedText = attributedText else { return }
@ -576,8 +592,7 @@ extension Label {
@objc public func addTappableLinkAttribute(range: NSRange, actionBlock: @escaping ActionBlock) {
setActionAttributes(range: range)
let accessibleAction = customAccessibilityAction(range: range)
clauses.append(ActionableClause(range: range, actionBlock: actionBlock, hash: accessibleAction?.hash ?? -1))
appendActionableClause(range: range, actionBlock: actionBlock)
}
/**
@ -592,9 +607,8 @@ extension Label {
@objc public func addTappableLinkAttribute(range: NSRange, actionMap: [AnyHashable: Any]?, additionalData: [AnyHashable: Any]?, delegateObject: DelegateObject?) {
setActionAttributes(range: range)
let accessibleAction = customAccessibilityAction(range: range)
let actionBlock = createActionBlockFrom(actionMap: actionMap, additionalData: additionalData, delegateObject: delegateObject)
clauses.append(ActionableClause(range: range, actionBlock: actionBlock, hash: accessibleAction?.hash ?? -1))
appendActionableClause(range: range, actionBlock: actionBlock)
}
@objc private func textLinkTapped(_ gesture: UITapGestureRecognizer) {
@ -668,7 +682,7 @@ extension Label {
@objc public func accessibilityCustomAction(_ action: UIAccessibilityCustomAction) {
for clause in clauses {
if action.hash == clause.hash {
if action.hash == clause.accessibilityID {
clause.performAction()
return
}

View File

@ -67,11 +67,11 @@ public typealias CoreObjectActionLoadPresentDelegate = MVMCoreActionDelegateProt
guard let label = label else { return }
let accessibleAction = UIAccessibilityCustomAction(name: actionText ?? "", target: label, selector: #selector(label.accessibilityCustomAction(_:)))
label.clauses = [Label.ActionableClause(range: actionRange, actionBlock: newActionBlock, hash: accessibleAction.hash)]
label.clauses = [Label.ActionableClause(range: actionRange, actionBlock: newActionBlock, accessibilityID: accessibleAction.hash)]
label.accessibilityCustomActions = [accessibleAction]
if label.accessibilityHint == nil {
label.accessibilityHint = MVMCoreUIUtility.hardcodedString(withKey: "plan_selector_int_swipe_action_hint")
label.accessibilityHint = MVMCoreUIUtility.hardcodedString(withKey: "swipe_to_select_with_action_hint")
}
}
}