https://github.com/octree/attributedtext
An elegant way to build NSAttributedString just like SwiftUI.
https://github.com/octree/attributedtext
ios macos nsattributedstring resultbuilder swift
Last synced: 4 months ago
JSON representation
An elegant way to build NSAttributedString just like SwiftUI.
- Host: GitHub
- URL: https://github.com/octree/attributedtext
- Owner: octree
- License: mit
- Created: 2021-04-20T08:10:45.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2022-06-27T08:35:37.000Z (over 3 years ago)
- Last Synced: 2025-01-12T09:59:56.332Z (12 months ago)
- Topics: ios, macos, nsattributedstring, resultbuilder, swift
- Language: Swift
- Homepage:
- Size: 195 KB
- Stars: 4
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# AttributedText
An elegant way to build `NSAttributedString` like SwiftUI.
Power by swift `Result Builder`
## Usage
```swift
let list = ["hello", "world"]
let attributedText: NSAttributedString = .Builder {
"@resultBuilder".bold.italic.foreground(color: .purple)
if #available(iOS 14, *) {
"Wow".lineHeight(20)
}
}
.build()
```
> Attachment and Loops
```swift
let list = ["谈笑风生", "香港记者", "人生经验", "无可奉告", "身经百战", "亦可赛艇", "图样图森破", "另请高明"]
textView.attributedText = .Builder {
Group {
attachment
" Result builder is awesome\n".bold
.fontSize(24)
.foreground(color: .label)
}
.lineSpacing(8)
for element in list {
element.font(.systemFont(ofSize: 16))
.foreground(color: .random)
.lineSpacing(4)
}
}
.build()
```

## Installation
### Swift Package Manager
* File > Swift Packages > Add Package Dependency
* Add https://github.com/octree/AttributedText.git
* Select "Up to Next Major" with "1.0.0"
## License
AttributedText is available under the MIT license. See the LICENSE file for more info.