{"id":13535191,"url":"https://github.com/ethanhuang13/NSAttributedStringBuilder","last_synced_at":"2025-04-02T00:32:49.116Z","repository":{"id":37686787,"uuid":"195989648","full_name":"ethanhuang13/NSAttributedStringBuilder","owner":"ethanhuang13","description":"Composing NSAttributedString with SwiftUI-style syntax","archived":false,"fork":false,"pushed_at":"2024-09-02T05:03:35.000Z","size":5470,"stargazers_count":754,"open_issues_count":3,"forks_count":35,"subscribers_count":13,"default_branch":"master","last_synced_at":"2025-03-24T02:30:48.787Z","etag":null,"topics":["functionbuilder","nsattributedstring","swift5-1","swiftui"],"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/ethanhuang13.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-07-09T10:47:03.000Z","updated_at":"2025-03-04T13:02:49.000Z","dependencies_parsed_at":"2024-06-21T02:15:11.978Z","dependency_job_id":"ad47e51d-3cb5-4d81-a96a-c27bb40ea33b","html_url":"https://github.com/ethanhuang13/NSAttributedStringBuilder","commit_stats":{"total_commits":53,"total_committers":2,"mean_commits":26.5,"dds":"0.15094339622641506","last_synced_commit":"270e77b329e33bc64171036c08722c0d7e3a1c40"},"previous_names":[],"tags_count":9,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ethanhuang13%2FNSAttributedStringBuilder","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ethanhuang13%2FNSAttributedStringBuilder/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ethanhuang13%2FNSAttributedStringBuilder/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ethanhuang13%2FNSAttributedStringBuilder/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ethanhuang13","download_url":"https://codeload.github.com/ethanhuang13/NSAttributedStringBuilder/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246413230,"owners_count":20773053,"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":["functionbuilder","nsattributedstring","swift5-1","swiftui"],"created_at":"2024-08-01T08:00:51.031Z","updated_at":"2025-04-02T00:32:49.083Z","avatar_url":"https://github.com/ethanhuang13.png","language":"Swift","readme":"# NSAttributedStringBuilder\n[![Build Status](https://github.com/ethanhuang13/NSAttributedStringBuilder/workflows/Swift/badge.svg)](https://github.com/ethanhuang13/NSAttributedStringBuilder/actions?workflow=Swift)\n[![codecov](https://codecov.io/gh/ethanhuang13/NSAttributedStringBuilder/branch/master/graph/badge.svg)](https://codecov.io/gh/ethanhuang13/NSAttributedStringBuilder)\n[![GitHub release](https://img.shields.io/github/release/ethanhuang13/nsattributedstringbuilder.svg)]()\n![GitHub top language](https://img.shields.io/github/languages/top/ethanhuang13/nsattributedstringbuilder.svg)\n[![License](https://img.shields.io/github/license/ethanhuang13/nsattributedstringbuilder.svg)](https://github.com/ethanhuang13/ladybug/blob/master/LICENSE)\n[![Twitter](https://img.shields.io/badge/Twitter-%40ethanhuang13-blue.svg)](https://twitter.com/ethanhuang13)\n[![Donate](https://img.shields.io/badge/Donate-PayPal-green.svg)](https://paypal.me/ethanhuang13) \n\nComposing `NSAttributedString` with SwiftUI-style syntax, powered by [Result Builder](https://forums.swift.org/t/function-builders/25167).\n\nProject Link: [https://github.com/ethanhuang13/NSAttributedStringBuilder](https://github.com/ethanhuang13/NSAttributedStringBuilder)\n\n## Features\n\n| | Features |\n| --- | --- |\n| 🐦 | Open source library written in Swift 5.4 |\n| 🍬 | SwiftUI-like syntax |\n| 💪 | Support most attributes in `NSAttributedString.Key` |\n| 📦 | Distribution with Swift Package Manager |\n| 🧪 | Fully tested code |\n| 🛠 | Continuously integrates in [Swift Source Compatibility Suite](https://github.com/apple/swift-source-compat-suite/pull/373) |\n\n## How to use?\n\nTraditionally we compose a `NSAttributedString` like this:\n\n```Swift\nlet mas = NSMutableAttributedString(string: \"\")\nmas.append(NSAttributedString(string: \"Hello world\", attributes: [.font: UIFont.systemFont(ofSize: 24), .foregroundColor: UIColor.red]))\nmas.append(NSAttributedString(string: \"\\n\"))\nmas.append(NSAttributedString(string: \"with Swift\", attributes: [.font: UIFont.systemFont(ofSize: 20), .foregroundColor: UIColor.orange]))\n\n```\nNow, with **NSAttributedStringBuilder**, we can use SwiftUI-like syntax to declare `NSAttributedString`:\n\n```Swift\nlet attributedString = NSAttributedString {\n    AText(\"Hello world\")\n        .font(.systemFont(ofSize: 24))\n        .foregroundColor(.red)\n    LineBreak()\n    AText(\"with Swift\")\n        .font(.systemFont(ofSize: 20))\n        .foregroundColor(.orange)\n}\n\n```\n\n## Requirements\nXcode 12.5. This project uses Swift 5.4 feature [Result Builder](https://github.com/apple/swift-evolution/blob/main/proposals/0289-result-builders.md).\n\n## Installation\n\n### Swift Package\nOpen your project in Xcode 12, navigate to **Menu -\u003e Swift Packages -\u003e Add Package Dependency** and enter [https://github.com/ethanhuang13/NSAttributedStringBuilder](https://github.com/ethanhuang13/NSAttributedStringBuilder) to install.\n\n### CocoaPods\nAdd `pod 'NSAttributedStringBuilder13'` to your `Podfile`. \n\n## SwiftUI Sample Project\nBesides clearer `NSAttributedString` syntax, since **NSAttributedStringBuilder** uses Result Builder it also enables API to build components in `UIViewRepresentable`(which embeds `UIView` in a SwiftUI `View`).\n\nJust like a SwiftUI `Text` takes a `String` as input, the purpose of `AttributedText` in the sample project is to take a `NSAttributedString` as input and render in SwiftUI.\n\nTo achieve this, `AttributedText.swift` uses `@NSAttributedStringBuilder` to support SwiftUI-style syntax:\n\n![AttributedText.swift](demo2.jpg)\n\nThen using an `AttributedText` will be like:\n![ContentView.swift](demo.jpg)\n\nOpen the sample in ***/SwiftUISampleApp/AttributedTextSample.xcodeproj*** and check `AttributedText`. It uses `UITextView`, you can also use `UILabel` or `NSTextView`. \n\n## TODO\n* Better tests for image attachment\n\n## Known Issue\n* `NSAttributedString` does not support link color, therefore `Link` component with a `.color()` modifier has no effect. Alternatively you need to specify in `UITextView.linkTextAttributes` or `.tintColor`. \n\n## Others\nInitially discussed on this [Twitter thread](https://twitter.com/ethanhuang13/status/1148135534826442752). Some code are inspired by [zonble](https://github.com/zonble/NSAttributedStringBuilder)🙏.\n","funding_links":["https://paypal.me/ethanhuang13"],"categories":["Swift-Evolution","swiftui","NSAttributedString"],"sub_categories":["0289-result-builders"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fethanhuang13%2FNSAttributedStringBuilder","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fethanhuang13%2FNSAttributedStringBuilder","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fethanhuang13%2FNSAttributedStringBuilder/lists"}