Skip to content

Commit 73728d1

Browse files
authored
Merge pull request #18 from cybozu/fix-hyperlink
Fixed a bug caused by hyperlinks
2 parents 7c2148a + d726f3c commit 73728d1

File tree

3 files changed

+42
-25
lines changed

3 files changed

+42
-25
lines changed

Sources/LicenseList/LegacyLicenseView.swift

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import SwiftUI
99

1010
public struct LegacyLicenseView: View {
11-
@State private var sentences = [LegacyLicenseSentence]()
11+
@State private var lines = [[LegacyLicenseSentence]]()
1212

1313
private let library: Library
1414

@@ -18,39 +18,45 @@ public struct LegacyLicenseView: View {
1818

1919
public var body: some View {
2020
ScrollView {
21-
VStack(spacing: 0) {
22-
ForEach(sentences, id: \.body) { sentence in
23-
if sentence.isHyperLink {
24-
hyperLinkText(sentence.body)
25-
} else {
21+
VStack(alignment: .leading, spacing: 0) {
22+
ForEach(lines.indices, id: \.self) { index in
23+
if lines[index].count == 1,
24+
let sentence = lines[index].first {
2625
Text(sentence.body)
2726
.font(.caption)
27+
} else {
28+
lines[index].reduce(Text("")) { result, sentence in
29+
if sentence.isHyperLink {
30+
return result + Text(sentence.body)
31+
.font(.caption)
32+
.foregroundColor(Color.blue)
33+
} else {
34+
return result + Text(sentence.body)
35+
.font(.caption)
36+
}
37+
}
38+
.onTapGesture {
39+
if let linkText = lines[index].first(where: { $0.isHyperLink })?.body,
40+
let url = URL(string: linkText),
41+
UIApplication.shared.canOpenURL(url) {
42+
UIApplication.shared.open(url)
43+
}
44+
}
2845
}
2946
}
3047
}
3148
.frame(maxWidth: .infinity, alignment: .leading)
3249
.padding()
3350
}
3451
.onAppear {
35-
sentences = resolve(library.licenseBody)
52+
lines = resolve(library.licenseBody)
3653
}
3754
.navigationBarTitleInlineIfPossible(library.name)
3855
}
3956

40-
private func hyperLinkText(_ linkText: String) -> some View {
41-
Text(linkText)
42-
.font(.caption)
43-
.foregroundColor(Color.blue)
44-
.onTapGesture {
45-
if let url = URL(string: linkText),
46-
UIApplication.shared.canOpenURL(url) {
47-
UIApplication.shared.open(url)
48-
}
49-
}
50-
}
51-
52-
private func resolve(_ inputText: String) -> [LegacyLicenseSentence] {
53-
let pattern: String = "https?://[A-Za-z0-9\\.\\-\\[\\]!@#$%&=+/?:_]+"
54-
return inputText.split(pattern)
57+
private func resolve(_ inputText: String) -> [[LegacyLicenseSentence]] {
58+
return inputText
59+
.components(separatedBy: .newlines)
60+
.map { $0.split(URL.regexPattern) }
5561
}
5662
}

Sources/LicenseList/LicenseView.swift

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,9 @@ public struct LicenseView: View {
3737

3838
private func attribute(_ inputText: String) -> AttributedString {
3939
var attributedText = AttributedString(inputText)
40-
let pattern: String = "https?://[A-Za-z0-9-._~:/?#\\[\\]@!$&'()*+,;%=]+"
41-
let urls: [URL?] = inputText.match(pattern)
40+
let urls: [URL?] = inputText.match(URL.regexPattern)
4241
.map { URL(string: String(inputText[$0])) }
43-
let ranges = attributedText.match(pattern)
42+
let ranges = attributedText.match(URL.regexPattern)
4443
for case (let range, let url?) in zip(ranges, urls) {
4544
attributedText[range].link = url
4645
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//
2+
// URL+Extension.swift
3+
//
4+
//
5+
// Created by ky0me22 on 2023/09/26.
6+
//
7+
8+
import Foundation
9+
10+
extension URL {
11+
static let regexPattern = "https?://[A-Za-z0-9-.!@#$%&=+/?:_~]+"
12+
}

0 commit comments

Comments
 (0)