{"id":22126547,"url":"https://github.com/kiarashvosough1999/cocoattributedstringbuilder","last_synced_at":"2025-07-25T16:33:34.210Z","repository":{"id":53335027,"uuid":"409748327","full_name":"kiarashvosough1999/CocoAttributedStringBuilder","owner":"kiarashvosough1999","description":"Elegant and Easy AttributedStringBuilder in Swift","archived":false,"fork":false,"pushed_at":"2022-08-04T18:49:11.000Z","size":4134,"stargazers_count":10,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-12-01T15:17:07.615Z","etag":null,"topics":["cocoapods","ios","propertywrapper","string","swift","uilabel"],"latest_commit_sha":null,"homepage":"","language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/kiarashvosough1999.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-09-23T21:27:46.000Z","updated_at":"2022-09-05T09:42:52.000Z","dependencies_parsed_at":"2022-09-05T00:51:56.247Z","dependency_job_id":null,"html_url":"https://github.com/kiarashvosough1999/CocoAttributedStringBuilder","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kiarashvosough1999%2FCocoAttributedStringBuilder","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kiarashvosough1999%2FCocoAttributedStringBuilder/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kiarashvosough1999%2FCocoAttributedStringBuilder/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kiarashvosough1999%2FCocoAttributedStringBuilder/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/kiarashvosough1999","download_url":"https://codeload.github.com/kiarashvosough1999/CocoAttributedStringBuilder/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":227598457,"owners_count":17791605,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cocoapods","ios","propertywrapper","string","swift","uilabel"],"created_at":"2024-12-01T16:58:54.786Z","updated_at":"2024-12-01T16:58:55.633Z","avatar_url":"https://github.com/kiarashvosough1999.png","language":"Swift","funding_links":[],"categories":[],"sub_categories":[],"readme":"\n\u003cp align=\"center\"\u003e\n  \u003cimg src=\"https://github.com/kiarashvosough1999/CocoAttributedStringBuilder/blob/master/resources/coco_icon.png\"\u003e\n\u003c/p\u003e\n\n![Swift](https://img.shields.io/badge/Swift-5.3_or_Higher-orange?style=flat-square)\n![Platforms](https://img.shields.io/badge/Platforms-iOS_9_or_Higher-yellowgreen?style=flat-square)\n![Swift](https://img.shields.io/badge/Swift-5.3_or_Higher-orange?style=flat-square)\n[![Twitter](https://img.shields.io/badge/Twitter-@Vosough_k-blue.svg?style=flat-square)](https://twitter.com/vosough_k)\n[![Linkedin](https://img.shields.io/badge/Linkedin-KiarashVosough-blue.svg?style=flat-square)](https://www.linkedin.com/in/kiarashvosough/)\n\nCocoAttributedStringBuilder: Elegant and Easy AttributedStringBuilder in Swift\n\n- [Features](#features)\n- [Requirements](#requirements)\n- [Installation](#installation)\n- [SampleProjects](#sample)\n- [Usage](#usage)\n- [Contributors](#contributors)\n- [License](#license)\n\n## Features\n\n- [x] Use resultBuilder to create attributes\n- [x] Support NSShadow Attributes\n- [x] Support NSTextAttachment Attributes\n- [x] Support NSParagraphStyle Attributes\n- [x] Support NSParagraphStyle's NextTabs\n- [x] Specify range for each attribute\n- [ ] Support if-statement on builders\n- [ ] Provide operator to define an attribute\n\n## Requirements\n\n| Platform | Minimum Swift Version | Installation | Status |\n| --- | --- | --- | --- |\n| iOS 9.0+ | 5.3 | [CocoaPods](#cocoapods) | Tested |\n\n## Installation\n\n### CocoaPods\n\n[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`:\n\n```ruby\npod 'CocoAttributedStringBuilder'\n```\n\n## Sample\n\nI 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!\n\n## Usage\n\nUsing Coco is so easy, It is inspired by SwiftUI and the very useful PropertyWrapper `@resultBuilder` in swift.\n\nYou just need to annotate the functions or properties which return `NSAttributedString` to use Coco provided builders.\n\n```swift\nimport CocoAttributedStringBuilder\n\n@CocoAttributedStringBuilder\nvar builder: NSAttributedString { }\n\n@CocoAttributedStringBuilder\nfunc build() -\u003e NSAttributedString { }\n```\n\n### Create Your First CocoAttributedString\n\nIn order to create an AttributedString on Coco, you should use `CocoString` which takes a `String` and a builderBlock arguments.\n\n\u003e String which was provided as an input argument is also available on the builderBlock to avoid outer variable or self capturing.\n\n```swift\nimport CocoAttributedStringBuilder\n\n@CocoAttributedStringBuilder\nvar builder: NSAttributedString {\n    CocoString(\"Test Builder\") { str in \n      // attributes comes here\n    }\n}\n\n@CocoAttributedStringBuilder\nfunc build() -\u003e NSAttributedString {\n    CocoString(\"Test Builder\") { str in \n      // attributes comes here\n    }\n}\n```\n\n\n## Attributes\n\nCoco supports four kind of Attributes.\n\n\u003e Each attribute is normally added to whole string, unless you specify the range of string you demand.\n\u003e\u003e Coco is not responsible for handling invalid ranges, so be more careful on specifying the ranges.\n\n| Builders            | Description |\n| :-------------      |:-------------|\n| `CocoAttribute`     | Provide a keyValue interface for `NSAttributedString.Key` |\n| `TextAttachment`    | Provide a builder block for `CocoAttachment` which is an interface for `NSTextAttachment` |\n| `ParagrapghStyle`   | Provide a builder block for `CocoParagraphStyle` which is an interface for `NSParagraphStyle` |\n| `TextTab`           | Provide a builder block for `CocoTextTab` which is an interface for `NSTextTab` |\n| `CocoShadow`        | Provide a builder block for `CocoShadow which` is an interface for `NSShadow` |\n\n```swift\nimport CocoAttributedStringBuilder\n\n@CocoAttributedStringBuilder\nfunc build() -\u003e NSAttributedString {\n    CocoString(\"Test Builder\") { str in\n        CocoAttribute.foregroundColor(.red)\n            .on(str.startIndex..\u003cstr.firstIndex(of: \"r\")!)\n\n        TextAttachment {\n            CocoAttachment.bounds(.infinite)\n        }\n        .within(str.startIndex..\u003cstr.firstIndex(of: \"r\")!)\n        \n        ParagrapghStyle {\n            CocoParagraphStyle.lineHeightMultiple(8)\n            CocoParagraphStyle.lineSpacing(2.3)\n            TextTab {\n                CocoTextTab.tab(textAlignment: .left, location: 5)\n                CocoTextTab.tab(textAlignment: .center, location: 5)\n            }\n        }\n        .within { str.startIndex..\u003cstr.firstIndex(of: \"h\")! }\n\n        Shadow {\n            CocoShadow.shadowOffset(.init(width: 1.5, height: 1))\n            CocoShadow.shadowColor(UIColor.black)\n        }\n        .within(str.startIndex..\u003cstr.firstIndex(of: \"d\")!)\n    }\n}\n```\n\n#### Using Meta-Type\n\nInstead of Using Each `Coco` Enums to use its Attributes, you can use BuilderBlock with meta-type of `Coco` Enum provided for that Block.\n\n```swift\nimport CocoAttributedStringBuilder\n\n@CocoAttributedStringBuilder\nfunc build() -\u003e NSAttributedString {\nCocoString(\"Test Builder\") { str, a in\n    a.foregroundColor(.red)\n        .within(str.startIndex..\u003cstr.firstIndex(of: \"r\")!)\n\n    TextAttachment { t in\n        t.bounds(.infinite)\n    }\n}\n```\n\n#### Use Concrete Types\n\nI recommend you to use either meta-type provides block or `Coco` Enums, but there is another way with one special condition.\nyou can use each attribute Concrete type directly in each block if and only if that type is unique on that Enum.\n\n\u003e On this example Bounds input argument is Unique in `CocoCocoAttachment`, so we can use its input concrete type to add as attribute,\n\u003e `CGRect` which is defined is consided as `b.bounds(.infinite)`\n```swift\nimport CocoAttributedStringBuilder\n\n@CocoAttributedStringBuilder\nfunc build() -\u003e NSAttributedString {\nCocoString(\"Test Builder\") { str, a in\n\n    TextAttachment { t in\n        CGRect.infinite\n    }\n}\n```\n\n## Contributors\n\nFeel free to share your ideas or any other problems. Pull requests are welcomed.\n\n## License\n\nCocoAttributedStringBuilder is released under an MIT license. See [LICENSE](https://github.com/kiarashvosough1999/CocoAttributedStringBuilder/blob/master/LICENSE) for more information.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkiarashvosough1999%2Fcocoattributedstringbuilder","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkiarashvosough1999%2Fcocoattributedstringbuilder","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkiarashvosough1999%2Fcocoattributedstringbuilder/lists"}