{"id":16694426,"url":"https://github.com/foxfriends/pretty-string","last_synced_at":"2025-05-15T21:35:50.981Z","repository":{"id":62451080,"uuid":"138086854","full_name":"foxfriends/pretty-string","owner":"foxfriends","description":"NSAttributedString is ugly. PrettyString is pretty.","archived":false,"fork":false,"pushed_at":"2019-11-28T06:32:25.000Z","size":28,"stargazers_count":4,"open_issues_count":0,"forks_count":1,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-11-18T04:58:50.235Z","etag":null,"topics":["attributedstring","formatting","ios"],"latest_commit_sha":null,"homepage":null,"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/foxfriends.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":"2018-06-20T21:17:50.000Z","updated_at":"2019-11-28T06:37:43.000Z","dependencies_parsed_at":"2022-11-01T22:46:23.849Z","dependency_job_id":null,"html_url":"https://github.com/foxfriends/pretty-string","commit_stats":null,"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/foxfriends%2Fpretty-string","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/foxfriends%2Fpretty-string/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/foxfriends%2Fpretty-string/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/foxfriends%2Fpretty-string/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/foxfriends","download_url":"https://codeload.github.com/foxfriends/pretty-string/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":225383917,"owners_count":17465877,"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":["attributedstring","formatting","ios"],"created_at":"2024-10-12T16:45:48.832Z","updated_at":"2024-11-19T15:56:27.582Z","avatar_url":"https://github.com/foxfriends.png","language":"Swift","readme":"# PrettyString\n\n[![CI Status](https://img.shields.io/travis/foxfriends/PrettyString.svg?style=flat)](https://travis-ci.org/foxfriends/PrettyString)\n[![Version](https://img.shields.io/cocoapods/v/PrettyString.svg?style=flat)](https://cocoapods.org/pods/PrettyString)\n[![License](https://img.shields.io/cocoapods/l/PrettyString.svg?style=flat)](https://cocoapods.org/pods/PrettyString)\n[![Platform](https://img.shields.io/cocoapods/p/PrettyString.svg?style=flat)](https://cocoapods.org/pods/PrettyString)\n\nPrettyString defines a highly customizable, and extremely simple syntax for making strings pretty.\n\nWhere XML, HTML and Markdown based alternatives exist, PrettyString provides significantly more\nflexibility than Markdown with a much cleaner syntax than HTML.\n\n## Usage\n\nPrettyString allows you to specify sections within your string which are to be styled using certain\nattributes. To denote a section uses the syntax `{name:text}`, like this:\n\n```swift\nlet string = \"Hello this is some {blue:blue text}\"\n```\n\nHere, the name is `blue`, and the text is `blue text`. What this name means, however, is entirely up\nto you. There are *no* built in styles. Fortunately defining a style is very simple:\n\n```swift\nlet config = PrettyString.Config(\n    base: [],\n    rules: [\n        PrettyString.Config.Rule(\n            name: \"blue\",\n            attributes: [\n                .foregroundColor(UIColor.blue)\n            ]\n        )\n    ]\n)\n```\n\nIt looks a little big, but it's really very simple. The config consists of two parts: the `base`,\nand some `rules`.\n\nThe base is just a list of `Attribute`s which should be applied to the entire string.\n\nThe `rules` are then a list of `Rule`s. Each rule has a name, such as `blue`, and then a list of\n`Attribute`s that should be applied when this name is encountered.\n\nOnce you have a string and a `Config`, you are ready to prettify your strings!\n\n```swift\nlet attributedString = try! string.prettify(config)\n```\n\nNote that the conversion from `String` to `NSAttributedString` *can* fail, and so you must use `try`\nor one of its variations to handle the error. The error that is thrown is of type\n`PrettyString.Error`, and may provide some hint as to why your string has failed to parse.\n\nIf you find yourself always using the same `Config` object, you can even set the default config so\nthat calling `prettify` with no arguments will use it automatically. That means, the\n`attributedString` below is the same as the one above!\n\n```swift\nPrettyString.Config.default = config\nlet attributedString = try! string.prettify()\n```\n\n## The attributes\n\nThe full list of attributes is as follows:\n\n```swift\nenum Attribute {\n    case attachment(NSTextAttachment)\n    case backgroundColor(UIColor)\n    case baselineOffset(Float)\n    case expansion(Float)\n    case font(UIFont)\n    case foregroundColor(UIColor)\n    case kern(Float)\n    case ligature(Int)\n    case link(URL)\n    case obliqueness(Float)\n    case paragraphStyle(NSParagraphStyle)\n    case shadow(NSShadow)\n    case strikethroughColor(UIColor)\n    case strikethroughStyle(Int)\n    case strokeColor(UIColor)\n    case strokeWidth(Float)\n    case textEffect(String)\n    case underlineColor(UIColor)\n    case underlineStyle(NSUnderlineStyle)\n    case writingDirection([Int])\n}\n```\n\nEach corresponds to the `NSAttributedStringKey` of the same name. Their usage should be pretty\nstraightforward.\n\n## Advanced Usage\n\n### Escape Characters\n\nAll the regular escape characters and unicode sequences should *just work*, but what about actually\nwriting the `{` or `}` character in your string? To do that, escape it with an extra `{` in front.\n\nThat is, this: `\"{{he said: hello{}\"` will actually give you the string `{he said: hello}` with no\nattributes, rather than trying to find a rule called `he said` and applying it to `hello{`.\n\n### Nesting\n\nNesting of the attributed sections works as well. The attributes from the inner-most section will\noverride any of the outer styles, similar to how named sections will override the base styles:\n\n`{green-italics:hello {red-and-bold:there}}` should show \"hello\" in green italics and \"there\" in\nbold red italics (assuming you named the rules well).\n\n### Rule Names\n\nThe actual rules can have any character in them **except** for `:`, since that is used to mark the\nend of the name. You can even have a rule with `{` or `}` in the name, so long as it's not the first\ncharacter. I really suggest against doing this though, and just stick to the usual letters,\nunderscores, and hyphens since that ends up being the most clear.\n\n### Explicit API\n\nIf you prefer not to use the extension to the `String` type, the `PrettyString` struct itself can be\nused directly:\n\n```swift\nlet string = \"This is some {blue:blue text}\"\nlet prettyString = PrettyString(string, config: config)\nlet attributedString = try! prettyString.parse()\n```\n\n## Requirements\n\nThis project uses Swift 4, and you should too.\n\n## Installation\n\nPrettyString is available through [CocoaPods](https://cocoapods.org). To install\nit, simply add the following line to your Podfile:\n\n```ruby\npod 'PrettyString', '~\u003e 0.1'\n```\n\n## Author\n\nCameron Eldridge, cameldridge+git@gmail.com\n\n## License\n\nPrettyString is available under the MIT license. See the LICENSE file for more info.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffoxfriends%2Fpretty-string","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffoxfriends%2Fpretty-string","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffoxfriends%2Fpretty-string/lists"}