https://github.com/kiarashvosough1999/cocoattributedstringbuilder
Elegant and Easy AttributedStringBuilder in Swift
https://github.com/kiarashvosough1999/cocoattributedstringbuilder
cocoapods ios propertywrapper string swift uilabel
Last synced: 3 months ago
JSON representation
Elegant and Easy AttributedStringBuilder in Swift
- Host: GitHub
- URL: https://github.com/kiarashvosough1999/cocoattributedstringbuilder
- Owner: kiarashvosough1999
- License: mit
- Created: 2021-09-23T21:27:46.000Z (about 4 years ago)
- Default Branch: master
- Last Pushed: 2022-08-04T18:49:11.000Z (about 3 years ago)
- Last Synced: 2024-12-01T15:17:07.615Z (10 months ago)
- Topics: cocoapods, ios, propertywrapper, string, swift, uilabel
- Language: Swift
- Homepage:
- Size: 3.94 MB
- Stars: 10
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
![]()



[](https://twitter.com/vosough_k)
[](https://www.linkedin.com/in/kiarashvosough/)CocoAttributedStringBuilder: Elegant and Easy AttributedStringBuilder in Swift
- [Features](#features)
- [Requirements](#requirements)
- [Installation](#installation)
- [SampleProjects](#sample)
- [Usage](#usage)
- [Contributors](#contributors)
- [License](#license)## Features
- [x] Use resultBuilder to create attributes
- [x] Support NSShadow Attributes
- [x] Support NSTextAttachment Attributes
- [x] Support NSParagraphStyle Attributes
- [x] Support NSParagraphStyle's NextTabs
- [x] Specify range for each attribute
- [ ] Support if-statement on builders
- [ ] Provide operator to define an attribute## Requirements
| Platform | Minimum Swift Version | Installation | Status |
| --- | --- | --- | --- |
| iOS 9.0+ | 5.3 | [CocoaPods](#cocoapods) | Tested |## Installation
### CocoaPods
[CocoaPods](https://cocoapods.org) is a dependency manager for Cocoa projects. For usage and installation instructions, visit their website. To integrate CocoAttributedStringBuilder into your Xcode project using CocoaPods, specify it in your `Podfile`:
```ruby
pod 'CocoAttributedStringBuilder'
```## Sample
I have provided one sample project in the repository. To use it clone the repo, Source files for these are in the `CocoAttributedStringBuilderExamples` directory in project navigator. Have fun!
## Usage
Using Coco is so easy, It is inspired by SwiftUI and the very useful PropertyWrapper `@resultBuilder` in swift.
You just need to annotate the functions or properties which return `NSAttributedString` to use Coco provided builders.
```swift
import CocoAttributedStringBuilder@CocoAttributedStringBuilder
var builder: NSAttributedString { }@CocoAttributedStringBuilder
func build() -> NSAttributedString { }
```### Create Your First CocoAttributedString
In order to create an AttributedString on Coco, you should use `CocoString` which takes a `String` and a builderBlock arguments.
> String which was provided as an input argument is also available on the builderBlock to avoid outer variable or self capturing.
```swift
import CocoAttributedStringBuilder@CocoAttributedStringBuilder
var builder: NSAttributedString {
CocoString("Test Builder") { str in
// attributes comes here
}
}@CocoAttributedStringBuilder
func build() -> NSAttributedString {
CocoString("Test Builder") { str in
// attributes comes here
}
}
```## Attributes
Coco supports four kind of Attributes.
> Each attribute is normally added to whole string, unless you specify the range of string you demand.
>> Coco is not responsible for handling invalid ranges, so be more careful on specifying the ranges.| Builders | Description |
| :------------- |:-------------|
| `CocoAttribute` | Provide a keyValue interface for `NSAttributedString.Key` |
| `TextAttachment` | Provide a builder block for `CocoAttachment` which is an interface for `NSTextAttachment` |
| `ParagrapghStyle` | Provide a builder block for `CocoParagraphStyle` which is an interface for `NSParagraphStyle` |
| `TextTab` | Provide a builder block for `CocoTextTab` which is an interface for `NSTextTab` |
| `CocoShadow` | Provide a builder block for `CocoShadow which` is an interface for `NSShadow` |```swift
import CocoAttributedStringBuilder@CocoAttributedStringBuilder
func build() -> NSAttributedString {
CocoString("Test Builder") { str in
CocoAttribute.foregroundColor(.red)
.on(str.startIndex.. NSAttributedString {
CocoString("Test Builder") { str, a in
a.foregroundColor(.red)
.within(str.startIndex.. On this example Bounds input argument is Unique in `CocoCocoAttachment`, so we can use its input concrete type to add as attribute,
> `CGRect` which is defined is consided as `b.bounds(.infinite)`
```swift
import CocoAttributedStringBuilder@CocoAttributedStringBuilder
func build() -> NSAttributedString {
CocoString("Test Builder") { str, a inTextAttachment { t in
CGRect.infinite
}
}
```## Contributors
Feel free to share your ideas or any other problems. Pull requests are welcomed.
## License
CocoAttributedStringBuilder is released under an MIT license. See [LICENSE](https://github.com/kiarashvosough1999/CocoAttributedStringBuilder/blob/master/LICENSE) for more information.