Skip to content

Commit 63f7b6c

Browse files
authored
Merge pull request #3 from Tunous/documentation
Port documentation from JoinedText library
2 parents 269d3a6 + 1b1f3dd commit 63f7b6c

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

Sources/TextBuilder/TextBuilder.swift

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,24 @@
11
import SwiftUI
22

3+
/// A custom attribute that constructs combined text views.
4+
///
5+
/// You can use ``TextBuilder`` as an attribute for text-producing properties
6+
/// or function parameters, allowing them to provide combined text views. For example,
7+
/// the following `loremIpsum` property will create a single styled text view with each
8+
/// text separated using eggplant emoji.
9+
///
10+
/// struct EggplantSeparator: TextBuilderSeparator {
11+
/// static var separator: String { " 🍆 " }
12+
/// }
13+
///
14+
/// @TextBuilder<EggplantSeparator>
15+
/// var loremIpsum: Text {
16+
/// Text("Lorem").underline().foregroundColor(.blue)
17+
/// Text("ipsum dolor")
18+
/// Text("sit").bold()
19+
/// Text("amet, consectetur")
20+
/// }
21+
///
322
@resultBuilder
423
public struct TextBuilder<Separator: TextBuilderSeparator> {
524
public static func buildArray(_ texts: [[Text]]) -> [Text] {

Sources/TextBuilder/TextExtensions.swift

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,19 @@ extension StringProtocol {
55
}
66

77
extension Sequence where Element == Text {
8+
9+
/// Returns a new `Text` by concatenating the elements of the sequence,
10+
///
11+
/// The following example shows how an array of `Text` views can be joined to a
12+
/// single `Text` view with comma-separated string:
13+
///
14+
/// let cast = [Text("Vivien"), Text("Marlon"), Text("Kim")]
15+
/// let list = cast.joined(separator: Text(", "))
16+
/// // Gives Text("Vivien, Marlon, Kim")
17+
///
18+
/// - Parameter separator: A `Text` view to insert between each of the elements
19+
/// in this sequence. By default there is no separator.
20+
/// - Returns: A single, concatenated `Text` view.
821
public func joined(separator: Text = Text("")) -> Text {
922
var isInitial = true
1023
return reduce(Text("")) { (result, text) in
@@ -18,10 +31,24 @@ extension Sequence where Element == Text {
1831
}
1932

2033
extension Text {
34+
35+
/// Creates a combined text view based on the given `content` by inserting
36+
/// `separator` text views between each received text component.
37+
///
38+
/// - Parameters:
39+
/// - separator: The text to use as a separator between received text components.
40+
/// By default there is no separator.
41+
/// - content: A text builder that creates text components.
2142
public init(separator: Text = Text(""), @BasicTextBuilder content: () -> [Text]) {
2243
self = content().joined(separator: separator)
2344
}
2445

46+
/// Creates a combined text view based on the given `content` by inserting
47+
/// `separator` string between each received text component.
48+
///
49+
/// - Parameters:
50+
/// - separator: The string to use as a separator between received text components.
51+
/// - content: A text builder that creates text components.
2552
public init<Separator: StringProtocol>(separator: Separator, @BasicTextBuilder content: () -> [Text]) {
2653
self.init(separator: Text(separator), content: content)
2754
}

0 commit comments

Comments
 (0)