{"id":16686309,"url":"https://github.com/evermeer/attributedtextview","last_synced_at":"2025-04-04T18:08:42.439Z","repository":{"id":45822854,"uuid":"75232971","full_name":"evermeer/AttributedTextView","owner":"evermeer","description":"Easiest way to create an attributed UITextView (with support for multiple links and from html)","archived":false,"fork":false,"pushed_at":"2023-10-12T14:57:57.000Z","size":633,"stargazers_count":441,"open_issues_count":6,"forks_count":55,"subscribers_count":8,"default_branch":"master","last_synced_at":"2024-10-19T19:36:26.608Z","etag":null,"topics":["attributedstring","links","paragraph-styling","styling","swift","uitextview"],"latest_commit_sha":null,"homepage":"","language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/evermeer.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,"governance":null,"roadmap":null,"authors":null}},"created_at":"2016-11-30T22:31:40.000Z","updated_at":"2024-06-03T08:32:45.000Z","dependencies_parsed_at":"2024-01-05T20:28:49.214Z","dependency_job_id":null,"html_url":"https://github.com/evermeer/AttributedTextView","commit_stats":{"total_commits":60,"total_committers":9,"mean_commits":6.666666666666667,"dds":0.2666666666666667,"last_synced_commit":"8f14ccfe04db0b4c23e863a32659c0571fc895be"},"previous_names":[],"tags_count":22,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/evermeer%2FAttributedTextView","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/evermeer%2FAttributedTextView/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/evermeer%2FAttributedTextView/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/evermeer%2FAttributedTextView/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/evermeer","download_url":"https://codeload.github.com/evermeer/AttributedTextView/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247226215,"owners_count":20904465,"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","links","paragraph-styling","styling","swift","uitextview"],"created_at":"2024-10-12T15:05:21.153Z","updated_at":"2025-04-04T18:08:42.410Z","avatar_url":"https://github.com/evermeer.png","language":"Swift","readme":"## AttributedTextView\n\n[![Issues](https://img.shields.io/github/issues-raw/evermeer/AttributedTextView.svg?style=flat)](https://github.com/evermeer/AttributedTextView/issues)\n[![Documentation](https://img.shields.io/badge/documented-99%-green.svg?style=flat)](http://cocoadocs.org/docsets/AttributedTextView)\n[![Stars](https://img.shields.io/github/stars/evermeer/AttributedTextView.svg?style=flat)](https://github.com/evermeer/AttributedTextView/stargazers)\n\n[![Version](https://img.shields.io/cocoapods/v/AttributedTextView.svg?style=flat)](http://cocoadocs.org/docsets/AttributedTextView)\n[![Swift Package Manager](https://img.shields.io/badge/Swift%20Package%20Manager-compatible-brightgreen.svg)](https://github.com/apple/swift-package-manager)\n[![Carthage compatible](https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat)](https://github.com/Carthage/Carthage)\n[![Language](https://img.shields.io/badge/language-swift%204-f48041.svg?style=flat)](https://developer.apple.com/swift)\n[![Platform](https://img.shields.io/cocoapods/p/AttributedTextView.svg?style=flat)](http://cocoadocs.org/docsets/AttributedTextView)\n[![License](https://img.shields.io/cocoapods/l/AttributedTextView.svg?style=flat)](http://cocoadocs.org/docsets/AttributedTextView)\n\n[![Git](https://img.shields.io/badge/GitHub-evermeer-blue.svg?style=flat)](https://github.com/evermeer)\n[![Twitter](https://img.shields.io/badge/twitter-@evermeer-blue.svg?style=flat)](http://twitter.com/evermeer)\n[![LinkedIn](https://img.shields.io/badge/linkedin-Edwin%20Vermeer-blue.svg?style=flat)](http://nl.linkedin.com/in/evermeer/en)\n[![Website](https://img.shields.io/badge/website-evict.nl-blue.svg?style=flat)](http://evict.nl)\n[![eMail](https://img.shields.io/badge/email-edwin@evict.nl-blue.svg?style=flat)](mailto:edwin@evict.nl?SUBJECT=About%20EVReflection)\n\n\nEasiest way to create an attributed UITextView (with support for multiple links and html).\n\nSee the demo app and the playground for detailed information how to use AttributedTextView\n\n- [Requirements](#requirements)\n- [Usage](#usage)\n\t- [General usage](#general-usage)\n\t- [Paragraph styling](#paragraph-styling)\n\t- [The active range](#the-active-range)\n\t- [Sample code](#sample-code)\n\t- [Use the attributedtext functionality on a UILabel](#use-the-attributedtext-functionality-on-a-uilabel)\n\t- [Extending AttributedTextView](#extending-attributedtextview)\n\t- [Decorating the Attributed object](#decorating-the-attributed-object)\n\t- [Creating your own custom label](#creating-your-own-custom-label)\n\t- [Creating your own custom textview](#creating-your-own-custom-textview)\n- [Installation](#installation)\n- [License](#license)\n- [My other libraries](#My-other-libraries)\n\n## Requirements\n\n- iOS 8.0+ \n- Xcode 8.0+\n\n## Usage\n\n### General usage\nIn interfacebuilder put an UITextView on the canvas and set the base class to AttributedTextView and create a referencing outlet to the a property in  your viewController. In the samples below we have called this property textView1. Always assign to the attributer property when you want to set something.\n\n### Paragraph styling\nYou do have to be aware that the paragraph functions will only be applied after calling the .paragraphApplyStyling function. On start the paragraph styling will use default styling. After each range change (what happens after .all, .match* or .append) the styling will be reset to the default.\n\n### The active range\nStyling will always be applied on the active range. When executing a function on a string, then that complete string will become the active range. If you use .append to add an other string, then that latest string will become the active range. When using the + sign then that will replaced by an append on 2 Attributer objects. All functions on those objects will first be performed before the append will be executed. So if you do an .all then still only one of the strings will be tha active range. You can use brackets to influence the order of execution.\n\nFor instance here all text will be size 20\n\n```swift\n(\"red\".red + \"blue\".blue).all.size(20)\n```\n\nAnd here only the text blue will be size 20\n\n```swift\n\"red\".red + \"blue\".blue.all.size(20)\n```\n\nAnd like this all text will be size 20\n\n```swift\n\"red\".red.append(\"blue\").blue.all.size(20)\n```\n\n### Clickable links\nWhen using AttributedTextView and creating links with .makeInteract, then you have to be aware that it will also automatically set the following properties which are needed for links to work.\n\n```swift\nisUserInteractionEnabled = true\nisSelectable = true\nisEditable = false\n```\n\n### Sample code\n\nHere is a sample of some basic functions:\n\n```swift\ntextView1.attributer =\n    \"1. \".red\n    .append(\"This is the first test. \").green\n    .append(\"Click on \").black\n    .append(\"evict.nl\").makeInteract { _ in\n        UIApplication.shared.open(URL(string: \"http://evict.nl\")!, options: [:], completionHandler: { completed in })\n    }.underline\n    .append(\" for testing links. \").black\n    .append(\"Next test\").underline.makeInteract { _ in\n        print(\"NEXT\")\n    }\n    .all.font(UIFont(name: \"SourceSansPro-Regular\", size: 16))\n    .setLinkColor(UIColor.purple) \n```\n\n![animated](https://github.com/evermeer/AttributedTextView/blob/master/Screenshots/Sample1.png?raw=true)\n\nSome more attributes and now using + instead of .append:\n\n```swift\ntextView1.attributer =\n    \"2. red, \".red.underline.underline(0x00ff00)\n    + \"green, \".green.fontName(\"Helvetica\").size(30)\n    + \"cyan, \".cyan.size(22)\n    + \"orange, \".orange.kern(10)\n    + \"blue, \".blue.strikethrough(3).baselineOffset(8)\n    + \"black.\".shadow(color: UIColor.gray, offset: CGSize(width: 2, height: 3), blurRadius: 3.0)\n```\n\n![animated](https://github.com/evermeer/AttributedTextView/blob/master/Screenshots/Sample2.png?raw=true)\n\nA match and matchAll sample:\n\n```swift\ntextView1.attributer = \"It is this or it is that where the word is is selected\".size(20)\n    .match(\"is\").underline.underline(UIColor.red)\n    .matchAll(\"is\").strikethrough(4)\n```\n\n![animated](https://github.com/evermeer/AttributedTextView/blob/master/Screenshots/Sample3.png?raw=true)\n\nA hashtags and mentions sample:\n\n```swift\ntextView1.attributer = \"@test: What #hashtags do we have in @evermeer #AtributedTextView library\"\n    .matchHashtags.underline\n    .matchMentions\n    .makeInteract { link in\n        UIApplication.shared.open(URL(string: \"https://twitter.com\\(link.replacingOccurrences(of: \"@\", with: \"\"))\")!, options: [:], completionHandler: { completed in })\n    }\n```\n\n![animated](https://github.com/evermeer/AttributedTextView/blob/master/Screenshots/Sample4.png?raw=true)\n\nA html sample:\n\n```swift\ntextView1.attributer = \"My name is: \u003cb\u003eEdwin\u003c/b\u003e\u003cbr/\u003eWith a bulet list\u003cbr/\u003e\u003cul\u003e\u003cli\u003eitem 1\u003c/li\u003e\u003cli\u003eitem 2\u003c/li\u003e\u003c/ul\u003e\".html\n```\n\n![animated](https://github.com/evermeer/AttributedTextView/blob/master/Screenshots/Sample6.png?raw=true)\n\nSome other text formating samples:\n\n```swift\ntextView1.attributer =  (\n    \"test stroke\".strokeWidth(2).strokeColor(UIColor.red)\n    + \"test stroke 2\\n\".strokeWidth(2).strokeColor(UIColor.blue)\n    + \"test strikethrough\".strikethrough(2).strikethroughColor(UIColor.red)\n    + \" test strikethrough 2\\n\".strikethrough(2).strikethroughColor(UIColor.yellow)\n    + \"letterpress \".letterpress\n    + \" obliquenes\\n\".obliqueness(0.4).backgroundColor(UIColor.cyan)\n    + \"expansion\\n\".expansion(0.8)\n    ).all.size(24)\n```\n\n![animated](https://github.com/evermeer/AttributedTextView/blob/master/Screenshots/Sample5.png?raw=true)\n\n\nParagraph formatting:\n\n```swift\ntextView1.attributer = (\n    \"The quick brown fox jumps over the lazy dog.\\nPack my box with five dozen liquor jugs.\\nSeveral fabulous dixieland jazz groups played with quick tempo.\"\n    .paragraphLineHeightMultiple(5)\n    .paragraphLineSpacing(6)\n    .paragraphMinimumLineHeight(15)\n    .paragraphMaximumLineHeight(50)\n    .paragraphLineSpacing(10)\n    .paragraphLineBreakModeWordWrapping\n    .paragraphFirstLineHeadIndent(20)\n    .paragraphApplyStyling\n    ).all.size(12)\n```\n\n\n\n\n### Use the attributedText functionality on a UILabel\nYou can also use the Attributer for your UILabel. You only can't use the makeInteract function:\n\n```swift\nlet myUILabel = UILabel()\nmyUILabel.attributedText = (\"Just \".red + \"some \".green + \"text.\".orange).attributedText\n```\n\n\n### Extending AttributedTextView\nIn the demo app you can see how you can extend the AttributedTextView with a custom property / function that will perform multiple actions. Here is a simple sample that will show you how you can create a myTitle property that will set multiple attributes:\n\n```swift\nextension Attributer {\n    open var myTitle: Attributer {\n        get {\n            return self.fontName(\"Arial\").size(28).color(0xffaa66).kern(5)\n        }\n    }\n}\n\npublic extension String {\n    var myTitle: Attributer {\n        get {\n            return attributer.myTitle\n        }\n    }\n}\n```\n\n### Decorating the Attributed object\nIn the demo app there is also a sample that shows you how you could decorate an Attributed object with default styling.\n\n```swift\n        attributedTextView.attributer = decorate(4) { content in return content\n            + \"This is our custom title\".myTitle\n        }\n```\n\nThe decorate function can then look something like this:\n\n```swift\n    func decorate(_ id: Int, _ builder: ((_ content: Attributer) -\u003e Attributer)) -\u003e Attributer {\n        var b = \"Sample \\(id + 1):\\n\\n\".red\n        b = builder(b) // Now add the content\n        return b\n    }\n```\n\n### Creating your own custom label\nThere is also an AttributedLabel class which derives from UILabel which makes it easy to create your own custom control that includes support for interfacebuilder. If you put a lable on a form in interfacebuilder and set it's class to your subclass (AttributedLabelSubclassDemo in the sample below) Then you will see the text formated in interface building according to what you have implemented in the configureAttributedLabel function. Here is a sample where a highlightText property is added so that a text can be constructed where that part is highlighted. \n\n```swift\nimport AttributedTextView\nimport UIKit\n\n@IBDesignable open class AttributedLabelSubclassDemo: AttributedLabel {\n\n    // Add this field in interfacebuilder and make sure the interface is updated after changes\n    @IBInspectable var highlightText: String? {\n        didSet { configureAttributedLabel() }\n    }\n\n    // Configure our custom styling.\n    override open func configureAttributedLabel() {\n        self.numberOfLines = 0\n        if let highlightText = self.highlightText {\n            self.attributedText = self.text?.green.match(highlightText).red.attributedText\n        } else {\n            self.attributedText = self.text?.green.attributedText\n        }\n        layoutIfNeeded()\n    }\n}\n```\n\nIn the demo app there is also a lable subclass for an icon list like this:\n![animated](https://github.com/evermeer/AttributedTextView/blob/master/Screenshots/IconList.png?raw=true)\n\nYou can find the code here:\n[Icon bulet list code](https://github.com/evermeer/AttributedTextView/blob/master/Demo/AttributedLabelSubclassDemo.swift#L52)\n\n\n### Creating your own custom textview\nYou could do the same as a label with the AttributedTextView (see previous paragraph). In the sample below 2 properties are entered into interfacebuilder the linkText is the part of the text what will be clickable and linkUrl will be the webpage that will be opened.\n\n```swift\nimport AttributedTextView\nimport UIKit\n\n@IBDesignable class AttributedTextViewSubclassDemo: AttributedTextView {\n\n    // Add this field in interfacebuilder and make sure the interface is updated after changes\n    @IBInspectable var linkText: String? {\n        didSet { configureAttributedTextView() }\n    }\n\n    // Add this field in interfacebuilder and make sure the interface is updated after changes\n    @IBInspectable var linkUrl: String? {\n        didSet { configureAttributedTextView() }\n    }\n\n    // Configure our custom styling.\n    override func configureAttributedTextView() {\n        if let text = self.text, let linkText = self.linkText, let linkUrl = self.linkUrl {\n            self.attributer = text.green.match(linkText).makeInteract { _ in\n                UIApplication.shared.open(URL(string: linkUrl)!, options: [:], completionHandler: { completed in })\n            }\n        } else {\n            self.attributer = (self.text ?? \"\").green\n        }\n        layoutIfNeeded()\n    }\n}\n```\n\n\n## Installation\n\n### CocoaPods\n\n[CocoaPods](http://cocoapods.org) is a dependency manager for Cocoa projects. You can install it with the following command:\n\n```bash\n$ gem install cocoapods\n```\n\n\u003e CocoaPods 1.1.0+ is required to build AttributedTextView 0.1.0+.\n\nTo integrate AttributedTextView into your Xcode project using CocoaPods, specify it in your `Podfile`:\n\n```ruby\nsource 'https://github.com/CocoaPods/Specs.git'\nplatform :ios, '8.0'\nuse_frameworks!\n\npod 'AttributedTextView', '~\u003e 0.5.1'\n```\n\nThen, run the following command:\n\n```bash\n$ pod install\n```\n\n### Carthage\n\n[Carthage](https://github.com/Carthage/Carthage) is a decentralized dependency manager that automates the process of adding frameworks to your Cocoa application.\n\nYou can install Carthage with [Homebrew](http://brew.sh/) using the following command:\n\n```bash\n$ brew update\n$ brew install carthage\n```\n\nTo integrate AttributedTextView into your Xcode project using Carthage, specify it in your `Cartfile`:\n\n```ogdl\ngithub \"evermeer/AttributedTextView\" ~\u003e 0.5.1\n```\n### Swift Package Manager\n\nTo use AttributedTextView as a [Swift Package Manager](https://swift.org/package-manager/) package just add the following URL in your project file:\n\n```\nhttps://github.com/evermeer/AttributedTextView\n```\n\nAnd specify a version from `1.4.1` and onwards. \n\n### Manually\n\nIf you prefer not to use either of the aforementioned dependency managers, you can integrate AttributedTextView into your project manually.\n\n#### Git Submodules\n\n- Open up Terminal, `cd` into your top-level project directory, and run the following command \"if\" your project is not initialized as a git repository:\n\n```bash\n$ git init\n```\n\n- Add AttributedTextView as a git [submodule](http://git-scm.com/docs/git-submodule) by running the following command:\n\n```bash\n$ git submodule add https://github.com/evermeer/AttributedTextView.git\n$ git submodule update --init --recursive\n```\n\n- Open the new `AttributedTextView` folder, and drag the `AttributedTextView.xcodeproj` into the Project Navigator of your application's Xcode project.\n\n\u003e It should appear nested underneath your application's blue project icon. Whether it is above or below all the other Xcode groups does not matter.\n\n- Select the `AttributedTextView.xcodeproj` in the Project Navigator and verify the deployment target matches that of your application target.\n- Next, select your application project in the Project Navigator (blue project icon) to navigate to the target configuration window and select the application target under the \"Targets\" heading in the sidebar.\n- In the tab bar at the top of that window, open the \"General\" panel.\n- Click on the `+` button under the \"Embedded Binaries\" section.\n- You will see two different `AttributedTextView.xcodeproj` folders each with two different versions of the `AttributedTextView.framework` nested inside a `Products` folder.\n\n\u003e It does not matter which `Products` folder you choose from.\n\n- Select the `AttributedTextView.framework`.\n\n- And that's it!\n\n\u003e The `AttributedTextView.framework` is automagically added as a target dependency, linked framework and embedded framework in a copy files build phase which is all you need to build on the simulator and a device.\n\n#### Embeded Binaries\n\n- Download the latest release from https://github.com/evermeer/AttributedTextView/releases\n- Next, select your application project in the Project Navigator (blue project icon) to navigate to the target configuration window and select the application target under the \"Targets\" heading in the sidebar.\n- In the tab bar at the top of that window, open the \"General\" panel.\n- Click on the `+` button under the \"Embedded Binaries\" section.\n- Add the downloaded `AttributedTextView.framework`.\n- And that's it!\n\n## License\n\nAttributedTextView is released under the MIT license. See [LICENSE](https://github.com/evermeer/AttributedTextView/blob/master/LICENSE) for details.\n\n## My other libraries:\nAlso see my other public source iOS libraries:\n\n- [EVReflection](https://github.com/evermeer/EVReflection) - Reflection based (Dictionary, CKRecord, JSON and XML) object mapping with extensions for Alamofire and Moya with RxSwift or ReactiveSwift \n- [EVCloudKitDao](https://github.com/evermeer/EVCloudKitDao) - Simplified access to Apple's CloudKit\n- [EVFaceTracker](https://github.com/evermeer/EVFaceTracker) - Calculate the distance and angle of your device with regards to your face in order to simulate a 3D effect\n- [EVURLCache](https://github.com/evermeer/EVURLCache) - a NSURLCache subclass for handling all web requests that use NSURLReques\n- [AlamofireOauth2](https://github.com/evermeer/AlamofireOauth2) - A swift implementation of OAuth2 using Alamofire\n- [EVWordPressAPI](https://github.com/evermeer/EVWordPressAPI) - Swift Implementation of the WordPress (Jetpack) API using AlamofireOauth2, AlomofireJsonToObjects and EVReflection (work in progress)\n- [PassportScanner](https://github.com/evermeer/PassportScanner) - Scan the MRZ code of a passport and extract the firstname, lastname, passport number, nationality, date of birth, expiration date and personal numer.\n- [AttributedTextView](https://github.com/evermeer/AttributedTextView) - Easiest way to create an attributed UITextView with support for multiple links (url, hashtags, mentions).\n\n\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fevermeer%2Fattributedtextview","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fevermeer%2Fattributedtextview","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fevermeer%2Fattributedtextview/lists"}