Merge branch 'bugfix/label-action' into mbruce/bugfix
This commit is contained in:
commit
5d21e500bc
@ -389,60 +389,100 @@ open class Label: UILabel, ViewProtocol, UserInfoable {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//--------------------------------------------------
|
||||||
|
// MARK: - Touch Events
|
||||||
|
//--------------------------------------------------
|
||||||
@objc private func textLinkTapped(_ gesture: UITapGestureRecognizer) {
|
@objc private func textLinkTapped(_ gesture: UITapGestureRecognizer) {
|
||||||
for actionable in actions {
|
let location = gesture.location(in: self)
|
||||||
// This determines if we tapped on the desired range of text.
|
if let action = actions.first(where: { isAction(for: location, inRange: $0.range) }) {
|
||||||
let location = gesture.location(in: self)
|
action.performAction()
|
||||||
if didTapActionInLabel(location, inRange: actionable.range) {
|
|
||||||
actionable.performAction()
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public func isAction(for location: CGPoint) -> Bool {
|
public func isAction(for location: CGPoint) -> Bool {
|
||||||
for actionable in actions {
|
actions.contains(where: {isAction(for: location, inRange: $0.range)})
|
||||||
if didTapActionInLabel(location, inRange: actionable.range) {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private func didTapActionInLabel(_ location: CGPoint, inRange targetRange: NSRange) -> Bool {
|
public func isAction(for location: CGPoint, inRange targetRange: NSRange) -> Bool {
|
||||||
|
guard let abstractContainer = abstractTextContainer() else { return false }
|
||||||
|
let textContainer = abstractContainer.textContainer
|
||||||
|
let layoutManager = abstractContainer.layoutManager
|
||||||
|
|
||||||
guard let attributedText else { return false }
|
let indexOfGlyph = layoutManager.glyphIndex(for: location, in: textContainer)
|
||||||
|
let intrinsicWidth = intrinsicContentSize.width
|
||||||
|
|
||||||
|
// Assert that tapped occured within acceptable bounds based on alignment.
|
||||||
|
switch textAlignment {
|
||||||
|
case .right:
|
||||||
|
if location.x < bounds.width - intrinsicWidth {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
case .center:
|
||||||
|
let halfBounds = bounds.width / 2
|
||||||
|
let halfIntrinsicWidth = intrinsicWidth / 2
|
||||||
|
|
||||||
|
if location.x > halfBounds + halfIntrinsicWidth {
|
||||||
|
return false
|
||||||
|
} else if location.x < halfBounds - halfIntrinsicWidth {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
default: // Left align
|
||||||
|
if location.x > intrinsicWidth {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Affirms that the tap occured in the desired rect of provided by the target range.
|
||||||
|
return layoutManager.boundingRect(forGlyphRange: targetRange, in: textContainer).contains(location)
|
||||||
|
&& NSLocationInRange(indexOfGlyph, targetRange)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
Provides a text container and layout manager of how the text would appear on screen.
|
||||||
|
They are used in tandem to derive low-level TextKit results of the label.
|
||||||
|
*/
|
||||||
|
public func abstractTextContainer() -> (textContainer: NSTextContainer, layoutManager: NSLayoutManager, textStorage: NSTextStorage)? {
|
||||||
|
|
||||||
|
// Must configure the attributed string to translate what would appear on screen to accurately analyze.
|
||||||
|
guard let attributedText = attributedText else { return nil }
|
||||||
|
|
||||||
|
let paragraph = NSMutableParagraphStyle()
|
||||||
|
paragraph.alignment = textAlignment
|
||||||
|
|
||||||
|
let stagedAttributedString = NSMutableAttributedString(attributedString: attributedText)
|
||||||
|
stagedAttributedString.addAttributes([NSAttributedString.Key.paragraphStyle: paragraph], range: NSRange(location: 0, length: attributedText.string.count))
|
||||||
|
|
||||||
|
let textStorage = NSTextStorage(attributedString: stagedAttributedString)
|
||||||
let layoutManager = NSLayoutManager()
|
let layoutManager = NSLayoutManager()
|
||||||
let textContainer = NSTextContainer(size: bounds.size)
|
let textContainer = NSTextContainer(size: .zero)
|
||||||
let textStorage = NSTextStorage(attributedString: attributedText)
|
|
||||||
layoutManager.addTextContainer(textContainer)
|
layoutManager.addTextContainer(textContainer)
|
||||||
textStorage.addLayoutManager(layoutManager)
|
textStorage.addLayoutManager(layoutManager)
|
||||||
|
|
||||||
let characterIndex = layoutManager.characterIndex(for: location, in: textContainer, fractionOfDistanceBetweenInsertionPoints: nil)
|
textContainer.lineFragmentPadding = 0.0
|
||||||
|
textContainer.lineBreakMode = lineBreakMode
|
||||||
guard let _ = attributedText.attribute(NSAttributedString.Key.action, at: characterIndex, effectiveRange: nil) as? String, characterIndex < attributedText.length else { return false }
|
textContainer.maximumNumberOfLines = numberOfLines
|
||||||
return true
|
textContainer.size = bounds.size
|
||||||
|
|
||||||
|
return (textContainer, layoutManager, textStorage)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//--------------------------------------------------
|
||||||
|
// MARK: - Accessibility
|
||||||
|
//--------------------------------------------------
|
||||||
private func customAccessibilityElement(text: String?, range: NSRange, accessibleText: String? = nil) -> AccessibilityActionElement? {
|
private func customAccessibilityElement(text: String?, range: NSRange, accessibleText: String? = nil) -> AccessibilityActionElement? {
|
||||||
|
|
||||||
guard let text = text, let attributedText else { return nil }
|
guard let text = text, let abstractContainer = abstractTextContainer() else { return nil }
|
||||||
|
|
||||||
|
let textContainer = abstractContainer.textContainer
|
||||||
|
let layoutManager = abstractContainer.layoutManager
|
||||||
|
|
||||||
let actionText = accessibleText ?? (text.isValid(range: range) ? NSString(string:text).substring(with: range) : text)
|
let actionText = accessibleText ?? (text.isValid(range: range) ? NSString(string:text).substring(with: range) : text)
|
||||||
|
|
||||||
// Calculate the frame of the substring
|
|
||||||
let layoutManager = NSLayoutManager()
|
|
||||||
let textContainer = NSTextContainer(size: bounds.size)
|
|
||||||
let textStorage = NSTextStorage(attributedString: attributedText)
|
|
||||||
layoutManager.addTextContainer(textContainer)
|
|
||||||
textStorage.addLayoutManager(layoutManager)
|
|
||||||
|
|
||||||
var glyphRange = NSRange()
|
var glyphRange = NSRange()
|
||||||
|
|
||||||
// Convert the range for the substring into a range of glyphs
|
// Convert the range for the substring into a range of glyphs
|
||||||
layoutManager.characterRange(forGlyphRange: range, actualGlyphRange: &glyphRange)
|
layoutManager.characterRange(forGlyphRange: range, actualGlyphRange: &glyphRange)
|
||||||
|
|
||||||
let substringBounds = layoutManager.boundingRect(forGlyphRange: glyphRange, in: textContainer)
|
let substringBounds = layoutManager.boundingRect(forGlyphRange: glyphRange, in: textContainer)
|
||||||
|
|
||||||
// Create custom accessibility element
|
// Create custom accessibility element
|
||||||
@ -456,10 +496,6 @@ open class Label: UILabel, ViewProtocol, UserInfoable {
|
|||||||
return element
|
return element
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
//--------------------------------------------------
|
|
||||||
// MARK: - Accessibility
|
|
||||||
//--------------------------------------------------
|
|
||||||
open var accessibilityAction: ((Label) -> Void)?
|
open var accessibilityAction: ((Label) -> Void)?
|
||||||
|
|
||||||
private var _isAccessibilityElement: Bool = false
|
private var _isAccessibilityElement: Bool = false
|
||||||
|
|||||||
@ -12,23 +12,11 @@ extension UITapGestureRecognizer {
|
|||||||
|
|
||||||
/// Determines if the touch event has a action attribute within the range given
|
/// Determines if the touch event has a action attribute within the range given
|
||||||
/// - Parameters:
|
/// - Parameters:
|
||||||
/// - label: UILabel in question
|
/// - label: Label in question
|
||||||
/// - targetRange: Range to look within
|
/// - targetRange: Range to look within
|
||||||
/// - Returns: Wether the range in the label has an action
|
/// - Returns: Wether the range in the label has an action
|
||||||
public func didTapActionInLabel(_ label: UILabel, inRange targetRange: NSRange) -> Bool {
|
public func didTapActionInLabel(_ label: Label, inRange targetRange: NSRange) -> Bool {
|
||||||
|
let tapLocation = location(in: label)
|
||||||
guard let attributedText = label.attributedText else { return false }
|
return label.isAction(for: tapLocation, inRange: targetRange)
|
||||||
|
|
||||||
let layoutManager = NSLayoutManager()
|
|
||||||
let textContainer = NSTextContainer(size: label.bounds.size)
|
|
||||||
let textStorage = NSTextStorage(attributedString: attributedText)
|
|
||||||
layoutManager.addTextContainer(textContainer)
|
|
||||||
textStorage.addLayoutManager(layoutManager)
|
|
||||||
|
|
||||||
let location = location(in: label)
|
|
||||||
let characterIndex = layoutManager.characterIndex(for: location, in: textContainer, fractionOfDistanceBetweenInsertionPoints: nil)
|
|
||||||
|
|
||||||
guard let _ = attributedText.attribute(NSAttributedString.Key.action, at: characterIndex, effectiveRange: nil) as? String, characterIndex < attributedText.length else { return false }
|
|
||||||
return true
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user