{"id":17160415,"url":"https://github.com/kazuhiro4949/stringstylizer","last_synced_at":"2025-04-13T13:30:31.648Z","repository":{"id":6902504,"uuid":"55659143","full_name":"kazuhiro4949/StringStylizer","owner":"kazuhiro4949","description":"Type strict builder class for NSAttributedString.","archived":false,"fork":false,"pushed_at":"2022-07-04T03:05:00.000Z","size":879,"stargazers_count":74,"open_issues_count":3,"forks_count":7,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-04-09T10:03:28.902Z","etag":null,"topics":["ios","nsattributedstring","swift"],"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/kazuhiro4949.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-04-07T03:03:02.000Z","updated_at":"2024-11-30T17:57:12.000Z","dependencies_parsed_at":"2022-11-28T10:34:05.363Z","dependency_job_id":null,"html_url":"https://github.com/kazuhiro4949/StringStylizer","commit_stats":null,"previous_names":[],"tags_count":19,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kazuhiro4949%2FStringStylizer","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kazuhiro4949%2FStringStylizer/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kazuhiro4949%2FStringStylizer/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kazuhiro4949%2FStringStylizer/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/kazuhiro4949","download_url":"https://codeload.github.com/kazuhiro4949/StringStylizer/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248720918,"owners_count":21151010,"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":["ios","nsattributedstring","swift"],"created_at":"2024-10-14T22:24:46.625Z","updated_at":"2025-04-13T13:30:31.610Z","avatar_url":"https://github.com/kazuhiro4949.png","language":"Swift","readme":"# StringStylizer\n\n[![Carthage compatible](https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat)](https://github.com/Carthage/Carthage)\n![Cocoapods compatible](https://cocoapod-badges.herokuapp.com/v/StringStylizer/1.1.1/badge.png)\n![Swift 5.0](https://img.shields.io/badge/Swift-5.0-orange.svg)\n\nType strict builder class for [NSAttributedString](https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSAttributedString_Class/). \n\n\n## What's this?\nStringStylizer makes NSAttributedString more intuitive with Method chain and Operator.\nBuilding NSAttributedString is so difficult because it requires us to remember attribute names and types. If you do that with StringStylizer, There is no need to remember them :smiley:\n\nNSAttributedString has the following format.\n```swift\nlet attr: [NSAttributedString.Key: Any] = [\n    .foregroundColor: UIColor.white,\n    .font: UIFont(name: \"HelveticaNeue\", size: 14)\n]\nlet str = NSAttributedString(string: \"some text\", attributes: attr)\n```\n\nStringStylizer enable developers to read and write their code in a linear manner.\nIf you wanna convert String to NSAttributedString which has some colors, sizes and fonts, you can write that as follows.\n```swift\nlet str = \"some text\".stylize().color(.white).size(14).font(.HelveticaNeue).attr\n```\n\n## Feature\n- [x] Type strict format\n- [x] Assigning ranges and attributes in a linear manner\n- [x] More readable than NSAttributedString\n\n## Requirements\n- iOS 8.0+\n- Swift 5.0~ or Swift 4.0~ or Swift 3.2~\n\n## Installation\n### Swift Package Manager\nThe Swift Package Manager is a tool for automating the distribution of Swift code and is integrated into the swift compiler.\n\nOnce you have your Swift package set up, adding StringStylizer as a dependency is as easy as adding it to the dependencies value of your Package.swift.\n\n```\ndependencies: [\n    .package(url: \"https://github.com/vanvo/StringStylizer.git\", .upToNextMajor(from: \"5.2.2\"))\n]\n```\n\n### Carthage\n\n+ Install Carthage from Homebrew\n```\n\u003e ruby -e \"$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)\"\n\u003e brew update\n\u003e brew install carthage\n```\n+ Move your project dir and create Cartfile\n```\n\u003e touch Cartfile\n```\n+ add the following line to Cartfile\n```\ngithub \"kazuhiro4949/StringStylizer\"\n```\n+ Create framework\n```\n\u003e carthage update --platform iOS\n```\n\n+ In Xcode, move to \"Genera \u003e Build Phase \u003e Linked Frameworks and Library\"\n+ Add the framework to your project\n+ Add a new run script and put the following code\n```\n/usr/local/bin/carthage copy-frameworks\n```\n+ Click \"+\" at Input file and Add the framework path\n```\n$(SRCROOT)/Carthage/Build/iOS/StringStylizer.framework\n```\n+ Write Import statement on your source file\n```\nimport StringStylizer\n```\n\n### CocoaPods\n+ Install CocoaPods\n```\n\u003e gem install cocoapods\n\u003e pod setup\n```\n+ Create Podfile\n```\n\u003e pod init\n```\n+ Edit Podfile\n```ruby\n# Uncomment this line to define a global platform for your project\nplatform :ios, '8.0'  # add\nuse_framework!  # add\n\ntarget 'MyAppName' do\n  pod 'StringStylizer' # add\nend\n\ntarget 'MyAppTests' do\n\nend\n\ntarget 'MyAppUITests'\n```\n\n+ Install\n\n```\n\u003e pod install\n```\nopen .xcworkspace\n\n## Example\n \n```swift\nlet label = UILabel(frame: CGRect(x: 0, y: 0, width: 100, height: 50))\n\n// build NSAttributedString.\nlet greeting = \"Hi, \".stylize().color(0x2200ee).size(12).font(.HelveticaNeue).attr\n\n// build NSAttributedString with ranges.\nlet msg = \"something happened \".stylize()\n    .range(0..\u003c9)        .color(0x009911).size(12).font(.HelveticaNeue)\n    .range(10..\u003cUInt.max).color(0xaa22cc).size(14).font(.HelveticaNeue_Bold).attr\n\n// build NSAttributedString objects and join them.\nlet name = \"to \".stylize().color(0x23abfc).size(12).font(.HelveticaNeue).attr +\n    \"you\".stylize().color(0x123456).size(14).font(.HelveticaNeue_Italic).underline(.double).attr\n    \n// build NSAttributedString objects with strikethrough and kerning applied.\nlet response = \"\\nHow \".stylize().attr +  \"boring\".stylize().strikeThrough(.single).attr +\n    \" exciting!\".stylize().kern(-2).attr\n```\n\nThis sample generates a styled label.\n \n\u003cimg width=\"350\" src=\"https://user-images.githubusercontent.com/2948712/58213150-3fe99980-7d45-11e9-8b4c-9da54ff156de.png\"\u003e\n\nOf course, you can wrap up the method chains. \n```swift\nextension StringStylizer {\n  func strong() -\u003e NSAttributedString {\n    return self\n            .color(0x123456)\n            .size(14)\n            .font(.HelveticaNeue_Italic)\n            .underline(.double)\n            .attr\n  }\n}\n\nlabel.attributedText = \"you\".stylize().strong()\n```\n\n## Usage\n#### 1. Convert String to StringStylizer object\n```swift\nlet firstStep = \"yay!\".stylize() // =\u003e StringStylizer\u003cStyling\u003e\n```\n\n##### 1a. Alternatively, use an optional String\n```swift\nlet optionalString:String? = nil\nlet firstStep = optionalString.stylize() // =\u003e StringStylizer\u003cStyling\u003e\n```\n#### 2. Call methods to select range. Then, StringStylizer change into \"NarrowDown\" state\n```swift\nlet secondStep = \"yay!\".stylize().range(0..\u003cUInt.max) // =\u003e StringStylizer\u003cNarrowDown\u003e\n```\n#### 3. Call methods to set attributes. Then, StringStylizer change into \"Styling\" state\n```swift\nlet thirdStep = \"yay!\".stylize().range(0..\u003cUInt.max).size(14) // =\u003e StringStylizer\u003cStyling\u003e\n```\n#### 4. Convert to NSAttributedString object.\n```swift\nlet fourthStep = \"yay!\".stylize().range(0..\u003cUInt.max).size(14).attr // =\u003e NSAttributedString\n```\n#### 5. Join another NSAttributedString object.\n```swift\nlet one     = \"yay!\".stylize().range(0..\u003cUInt.max).size(14).attr\nlet another = \" yay!\".stylize().color(0xffffff).attr\nlet fifthStep = one + another // =\u003e NSAttributedString\n```\n\nThat's it!\n\n## Architecture\nStringStylizer is based on **\"Builder Pattern\"** (Effective Java version). In addition, it has states managed by **\"Phantom Type\"**.\n\n\u003cimg width=\"800\" src=\"https://cloud.githubusercontent.com/assets/18320004/18075348/f038fec8-6eae-11e6-8e9c-98c0fa39bcde.png\"\u003e\n\nBecause of them, we are able to\n- write our code in a linear manner\n- call proper methods depending on the situation.\n\n## License\n\nCopyright (c) 2016 Kazuhiro Hayashi\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in\nall copies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\nTHE SOFTWARE.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkazuhiro4949%2Fstringstylizer","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkazuhiro4949%2Fstringstylizer","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkazuhiro4949%2Fstringstylizer/lists"}