https://github.com/simonbs/textdiffing
🧬 TextDiffing helps you create an AttributedString / NSAttributedString to visualize differences between texts.
https://github.com/simonbs/textdiffing
Last synced: 4 months ago
JSON representation
🧬 TextDiffing helps you create an AttributedString / NSAttributedString to visualize differences between texts.
- Host: GitHub
- URL: https://github.com/simonbs/textdiffing
- Owner: simonbs
- License: mit
- Created: 2025-06-03T03:40:43.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2026-01-31T14:59:03.000Z (5 months ago)
- Last Synced: 2026-02-01T02:33:23.032Z (5 months ago)
- Language: Swift
- Homepage:
- Size: 392 KB
- Stars: 153
- Watchers: 2
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
# 🧬 TextDiffing
TextDiffing helps you create an AttributedString / NSAttributedString to visualize differences between texts.
[](https://swiftpackageindex.com/simonbs/TextDiffing)
[](https://swiftpackageindex.com/simonbs/TextDiffing)\
[](https://github.com/simonbs/TextDiffing/actions/workflows/build.yml)
[](https://github.com/simonbs/TextDiffing/actions/workflows/swiftlint.yml)
[](https://github.com/simonbs/TextDiffing/actions/workflows/test.yml)
## ✨ Features
- Compare two strings and generate [AttributedString](https://developer.apple.com/documentation/foundation/attributedstring) / [NSAttributedString](https://developer.apple.com/documentation/foundation/nsattributedstring) highlighting differences
- Customize appearance of changes
- Supports word- and character-level diffing
- Lightweight and easy to integrate
## 📦 Adding the Package
TextDiffing is distributed using [Swift Package Manager](https://www.swift.org/documentation/package-manager/). Install TextDiffing in a project by adding it as a dependency in your Package.swift manifest or through “Package Dependencies” in project settings.
```swift
let package = Package(
dependencies: [
.package(url: "git@github.com:simonbs/textdiffing.git", from: "1.0.2")
]
)
```
## 📖 Documentation
The documentation is available on Swift Package Index.
## 🚀 Getting Started
Use the TextDiffer to compare two strings.
```swift
let result = TextDiffer.diff(text, and: otherText)
```
The returned TextDiffResult has two properties:
|Property|Description|
|-|-|
|`attributedString`|The formatted `AttributedString` representing the differences.|
|`changeCount`|The number of changes (insertions or removals) between the texts.|
```swift
let attributedString = result.attributedString
let changeCount = result.changeCount
```
The `TextDiffer.diff(_:and:)` method also takes the following options.
|Option|Description|
|-|-|
|`strikethroughRemovedText`|Adds a strikethrough to removed text.|
|`tokenizeByCharacter`|Tokenizes the input by individual characters.|
|`tokenizeByWord`|Tokenizes the input by words (default).|
By default, text is tokenized by word. You can combine multiple options to customize behavior.
```swift
let result = TextDiffer.diff(text, and: otherText, options: [.tokenizeByCharacter, .strikethroughRemovedText])
```
You can customize the appearance of inserted and removed text by providing your own TextDiffStyle. This lets you control the background color used for visual highlighting.
```swift
let style = TextDiffStyle(
insertedBackground: UIColor.systemGreen.withAlphaComponent(0.3),
removedBackground: UIColor.systemRed.withAlphaComponent(0.3)
)
let result = TextDiffer.diff(text, and: otherText, style: style)
```
You may also use the extensions on NSAttributedString and AttributedString.
```swift
let attributedString = AttributedString(diffing: text, and: otherText)
let attributedString = NSAttributedString(diffing: text, and: otherText)
```
The initializers provided by the extensions also optionally take a style and options.
```swift
let attributedString = AttributedString(diffing: text, and: otherText, style: style, options: options)
let attributedString = NSAttributedString(diffing: text, and: otherText, style: style, options: options)
```