From 8614e98a3a7845b145595e58c2ff061f2faca552 Mon Sep 17 00:00:00 2001 From: kaijin Date: Thu, 3 Jun 2021 18:52:34 +0800 Subject: [PATCH 1/2] add condition and loop stringBuilder --- .../NSAttributedStringBuilder.swift | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/Sources/NSAttributedStringBuilder/NSAttributedStringBuilder.swift b/Sources/NSAttributedStringBuilder/NSAttributedStringBuilder.swift index d823859..45ec722 100644 --- a/Sources/NSAttributedStringBuilder/NSAttributedStringBuilder.swift +++ b/Sources/NSAttributedStringBuilder/NSAttributedStringBuilder.swift @@ -13,6 +13,16 @@ public typealias Attributes = [NSAttributedString.Key: Any] @resultBuilder public enum NSAttributedStringBuilder { + public static func buildOptional(_ component: NSAttributedString?) -> Component { + + if let content = component { + return AText(content.string, attributes: content.attributesAll) + } + else{ + return AText("") + } + } + public static func buildBlock(_ components: Component...) -> NSAttributedString { let mas = NSMutableAttributedString(string: "") for component in components { @@ -20,10 +30,32 @@ public enum NSAttributedStringBuilder { } return mas } + + public static func buildEither(first component: NSAttributedString) -> Component { + return AText(component.string, attributes: component.attributesAll) + } + + public static func buildEither(second component: NSAttributedString) -> Component { + return AText(component.string, attributes: component.attributesAll) + } + + public static func buildArray(_ components: [NSAttributedString]) -> Component { + let mas = NSMutableAttributedString(string: "") + for component in components { + mas.append(component) + } + return AText(mas.string, attributes: mas.attributes(at: 0, effectiveRange: nil)) + } + } public extension NSAttributedString { convenience init(@NSAttributedStringBuilder _ builder: () -> NSAttributedString) { self.init(attributedString: builder()) } + + var attributesAll:[NSAttributedString.Key : Any] { + self.attributes(at: 0, effectiveRange: nil) + } + } From 893284c63baa845d0e99f092b663581228593e8d Mon Sep 17 00:00:00 2001 From: kaijin Date: Thu, 3 Jun 2021 19:06:12 +0800 Subject: [PATCH 2/2] Update README.md --- README.md | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/README.md b/README.md index 5656444..58fbb77 100644 --- a/README.md +++ b/README.md @@ -48,7 +48,35 @@ let attributedString = NSAttributedString { ``` +Add Conditional String Builder and Loop String Builder + +```Swift +@NSAttributedStringBuilder +var attributedString:NSAttributedString{ + AText("Hello world") + .attribute(.foregroundColor, value: Color.blue) + + for character in ["a", "b", "c", "d"] { + AText(character) + .backgroundColor(UIColor.red) + .alignment(.left) + } + + if condition { + AText("True") + .foregroundColor(UIColor.black) + } + else{ + AText("False") + .backgroundColor(UIColor.yellow) + } +} +``` + + + ## Requirements + Xcode 12.5. This project uses Swift 5.4 feature [Result Builder](https://github.com/apple/swift-evolution/blob/main/proposals/0289-result-builders.md). ## Installation