Skip to content

Commit

Permalink
refactoring link handling into a single function
Browse files Browse the repository at this point in the history
  • Loading branch information
RoyalPineapple committed Nov 28, 2023
1 parent 7f3cad4 commit ca3b0a6
Showing 1 changed file with 9 additions and 10 deletions.
19 changes: 9 additions & 10 deletions BlueprintUICommonControls/Sources/AttributedLabel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -263,13 +263,7 @@ extension AttributedLabel {

if !isMeasuring {
if previousAttributedText != attributedText {
links = attributedLinks(in: model.attributedText) + detectedDataLinks(in: model.attributedText)

updateAccessibilityLinks(
for: links,
in: model.attributedText,
linkAccessibilityLabel: environment.linkAccessibilityLabel
)
updateAccessibilityLinks(linkAccessibilityLabel: environment.linkAccessibilityLabel)
}

if let shadow = model.shadow {
Expand Down Expand Up @@ -506,11 +500,16 @@ extension AttributedLabel {
}

private func updateAccessibilityLinks(
for links: [Link],
in string: NSAttributedString,
linkAccessibilityLabel: String?
) {

guard let string = attributedText else {
accessibilityLinks = []
return
}

links = attributedLinks(in: string) + detectedDataLinks(in: string)

accessibilityLinks = links
.sorted(by: { $0.range.location < $1.range.location })
.compactMap { link in
Expand All @@ -530,7 +529,7 @@ extension AttributedLabel {
let localizedLinkString = linkAccessibilityLabel,
!links.isEmpty else { return }

let insertionString = " [\(localizedLinkString)] "
let insertionString = "[\(localizedLinkString)] "
let reversed = links.sorted { $0.range.location > $1.range.location }
for link in reversed {
let insertionPoint = label.index(label.startIndex, offsetBy: link.range.location + link.range.length)
Expand Down

0 comments on commit ca3b0a6

Please sign in to comment.