Skip to content

Commit 25c6ae9

Browse files
committed
Initial work on adding support for links.
1 parent 8486140 commit 25c6ae9

File tree

4 files changed

+45
-27
lines changed

4 files changed

+45
-27
lines changed

Package.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
// swift-tools-version:5.3
1+
// swift-tools-version:5.5
22
// The swift-tools-version declares the minimum version of Swift required to build this package.
33

44
import PackageDescription
55

66
let package = Package(
77
name: "XMLText",
88
platforms: [
9-
.iOS(.v14), .macOS(.v11), .tvOS(.v14), .watchOS(.v7)
9+
.iOS(.v15), .macOS(.v12), .tvOS(.v14), .watchOS(.v7)
1010
],
1111
products: [
1212
.library(

Sources/XMLText/Style/Style.swift

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,20 @@ public class Style: StyleProtocol {
3030

3131
public var strikethroughColor: Color?
3232

33+
public var strikethroughStyle: NSUnderlineStyle?
34+
3335
public var underlineColor: Color?
3436

37+
public var underlineStyle: NSUnderlineStyle?
38+
3539
public var kerning: CGFloat?
3640

3741
public var tracking: CGFloat?
3842

3943
public var baselineOffset: CGFloat?
4044

45+
public var link: URL?
46+
4147
public init(_ handler: ((Style) -> Void)? = nil) {
4248
handler?(self)
4349
}
@@ -51,11 +57,17 @@ public class Style: StyleProtocol {
5157
result.append(.foregroundColor(foregroundColor))
5258
}
5359
if let strikethroughColor = strikethroughColor {
54-
result.append(.strikethrough(strikethroughColor))
60+
result.append(.strikethroughColor(strikethroughColor))
61+
}
62+
if let strikethroughStyle = strikethroughStyle {
63+
result.append(.strikethroughStyle(strikethroughStyle))
5564
}
5665
if let underlineColor = underlineColor {
5766
result.append(.underline(underlineColor))
5867
}
68+
if let underlineStyle = underlineStyle {
69+
result.append(.underlineStyle(underlineStyle))
70+
}
5971
if let kerning = kerning {
6072
result.append(.kerning(kerning))
6173
}
@@ -65,6 +77,9 @@ public class Style: StyleProtocol {
6577
if let baselineOffset = baselineOffset {
6678
result.append(.baselineOffset(baselineOffset))
6779
}
80+
if let link = link {
81+
result.append(.link(link))
82+
}
6883
return result
6984
}
7085
}

Sources/XMLText/Style/StyleProtocol.swift

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -25,46 +25,48 @@ import SwiftUI
2525
public enum StyleModifier {
2626
case font(SwiftUI.Font)
2727
case foregroundColor(Color)
28-
case strikethrough(Color)
28+
case strikethroughColor(Color)
29+
case strikethroughStyle(NSUnderlineStyle)
2930
case underline(Color)
31+
case underlineStyle(NSUnderlineStyle)
3032
case kerning(CGFloat)
3133
case tracking(CGFloat)
3234
case baselineOffset(CGFloat)
35+
case link(URL)
3336
}
3437

3538
public protocol StyleProtocol {
3639
var modifiers: [StyleModifier] { get }
3740

38-
func add(to source: Text) -> Text
41+
func add(to source: AttributedString) -> AttributedString
3942
}
4043

4144
public extension StyleProtocol {
4245

43-
func add(to source: Text) -> Text {
46+
func add(to source: AttributedString) -> AttributedString {
4447
var result = source
4548
for modifier in self.modifiers {
4649
switch modifier {
4750
case .font(let font):
48-
result = result
49-
.font(font)
51+
result.font = font
5052
case .foregroundColor(let color):
51-
result = result
52-
.foregroundColor(color)
53-
case .strikethrough(let color):
54-
result = result
55-
.strikethrough(true, color: color)
53+
result.foregroundColor = color
54+
case .strikethroughColor(let color):
55+
result.strikethroughColor = .init(color)
56+
case .strikethroughStyle(let style):
57+
result.strikethroughStyle = style
5658
case .underline(let color):
57-
result = result
58-
.underline(true, color: color)
59+
result.underlineColor = .init(color)
60+
case .underlineStyle(let style):
61+
result.underlineStyle = style
5962
case .kerning(let kerning):
60-
result = result
61-
.kerning(kerning)
63+
result.kern = kerning
6264
case .tracking(let tracking):
63-
result = result
64-
.tracking(tracking)
65+
result.tracking = tracking
6566
case .baselineOffset(let baselineOffset):
66-
result = result
67-
.baselineOffset(baselineOffset)
67+
result.baselineOffset = baselineOffset
68+
case .link(let link):
69+
result.link = link
6870
}
6971
}
7072
return result

Sources/XMLText/Text.XMLString.swift

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,21 @@ public extension Text {
1717
/// - styleGroup: Style group used for styling.
1818
init(xmlString: String, styleGroup: StyleGroup) {
1919
do {
20-
var text = Text("")
20+
var text = AttributedString()
2121
let xmlParser = XMLTextBuilder(
2222
styleGroup: styleGroup,
2323
string: xmlString,
2424
didFindNewString: { string, styles in
25-
var currentText = Text(string)
26-
27-
for style in styles.reversed() {
25+
var currentText = AttributedString(string)
26+
if let style = styles.last {
2827
currentText = style.add(to: currentText)
2928
}
30-
text = text + currentText
29+
text += currentText
3130
}
3231
)
3332
if let xmlParser = xmlParser {
3433
try xmlParser.parse()
35-
self = text
34+
self = Text(text)
3635
} else {
3736
self = Text(xmlString)
3837
}
@@ -121,12 +120,14 @@ struct Text_XMLString_Previews: PreviewProvider {
121120
style.font = Font.italic(.system(size: 20))()
122121
style.foregroundColor = .blue
123122
style.strikethroughColor = .yellow
123+
style.strikethroughStyle = .single
124124
}
125125

126126
let boldStyle = Style { style in
127127
style.font = Font.bold(.system(size: 20))()
128128
style.foregroundColor = .yellow
129129
style.underlineColor = .red
130+
style.underlineStyle = .single
130131
}
131132

132133
return .init(

0 commit comments

Comments
 (0)