This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@_functionBuilder | |
struct AttributedStringBuilder { | |
... | |
static func buildIf(_ segment: NSAttributedString?) -> NSAttributedString { | |
segment ?? NSAttributedString() | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
HStack { | |
if greeting != nil { | |
Text("\(greeting!) ") | |
} | |
Text("World") | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
NSAttributedString { | |
"Hello " | |
.foregroundColor(.red) | |
.font(UIFont.systemFont(ofSize: 10.0)) | |
"World" | |
.foregroundColor(.green) | |
.underline(.orange, style: .thick) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension String { | |
func foregroundColor(_ color: UIColor) -> NSAttributedString { | |
NSAttributedString(string: self, attributes: [.foregroundColor : color]) | |
} | |
func background(_ color: UIColor) -> NSAttributedString { | |
NSAttributedString(string: self, attributes: [.backgroundColor: color]) | |
} | |
func underline(_ color: UIColor, style: NSUnderlineStyle = .single) -> NSAttributedString { | |
NSAttributedString(string: self, attributes: [.underlineColor: color, .underlineStyle: style.rawValue]) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension String { | |
func colored(_ color: UIColor) -> NSAttributedString { | |
NSAttributedString(string: self, attributes: [.foregroundColor : color]) | |
} | |
func background(_ color: UIColor) -> NSAttributedString { | |
NSAttributedString(string: self, attributes: [.backgroundColor: color]) | |
} | |
func underline(_ color: UIColor, style: NSUnderlineStyle = .single) -> NSAttributedString { | |
NSAttributedString(string: self, attributes: [.underlineColor: color, .underlineStyle: style.rawValue]) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension NSAttributedString { | |
convenience init(@AttributedStringBuilder _ content: () -> NSAttributedString) { | |
self.init(attributedString: content()) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@_functionBuilder | |
struct AttributedStringBuilder { | |
static func buildBlock(_ segments: NSAttributedString...) -> NSAttributedString { | |
let string = NSMutableAttributedString() | |
segments.forEach { string.append($0) } | |
return string | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
NSAttributedString { | |
"Hello " | |
.color(.red) | |
"World" | |
.color(.blue) | |
.underline(.blue) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@_functionBuilder | |
struct GreetingBuilder { | |
static func buildBlock(_ items: String...) -> [String] { | |
return items.map { "Hello \($0)" } | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func combineWords() -> TupleView<(Text, Text)> { | |
let _a = Text("Hello") | |
let _b = Text("World") | |
return ViewBuilder.buildBlock(_a, _b) | |
} |