A powerful SwiftUI view component for comparing and displaying text differences with unified diff support.
📖 中文文档
- Text Diff Comparison: Compare two text strings and highlight differences
- Unified Diff Parser: Parse unified diff format from Git and other tools
- Syntax Highlighting: Support for multiple programming languages
- Collapsible Blocks: Automatically collapse unchanged sections
- Multiple View Modes: Unified diff, split view, original, and modified text
- Line Numbers: Accurate line number mapping
- Cross-platform: Supports macOS and iOS
Add this package to your project via Xcode:
- In Xcode, go to File > Swift Packages > Add Package Dependency
- Enter the repository URL
- Select the main branch
- Click Add Package
Or add it to your Package.swift:
dependencies: [
.package(
url: "https://github.com/your-username/MagicDiffView.git",
branch: "main"
)
]import SwiftUI
import MagicDiffView
struct ContentView: View {
var body: some View {
MagicDiffView(
oldText: "Hello World\nThis is line 2",
newText: "Hello Swift\nThis is line 2\nNew line 3"
)
}
}import MagicDiffView
struct GitDiffView: View {
let gitDiffOutput: String = """
diff --git a/file.txt b/file.txt
@@ -1,3 +1,3 @@
Line 1
-Line 2 old
+Line 2 new
Line 3
"""
var body: some View {
let diffLines = MyersDiffAlgorithm.parseUnifiedDiffSafely(gitDiffOutput)
return MagicDiffView(diffLines: diffLines)
}
}- iOS 17.0+ or macOS 14.0+
- Swift 5.9+
- Xcode 15.0+
MIT License - see LICENSE for details.
Contributions are welcome! Please feel free to submit a Pull Request.