ivan-magda/swiftui-expandable-text
Drop-in SwiftUI text that expands on tap. Markdown, animations, RTL-handled.
Features
- Markdown rendering support (bold, italic, ~~strikethrough~~,
code, links) - Expand text with customizable animation
- Automatically detects text truncation
- Customizable "more" button text, style, and font
- Supports various text styling options
- Works across iOS, macOS, tvOS, and watchOS
- Smooth gradient truncation effect
- RTL language support
Requirements
- iOS 15.0+ / macOS 12.0+ / tvOS 15.0+ / watchOS 8.0+
- Swift 6.0+
- Xcode 16.2+
Installation
Swift Package Manager
Add the following to your Package.swift file:
dependencies: [
.package(url: "https://github.com/ivan-magda/swiftui-expandable-text.git", from: "2.0.0")
]Or add it directly through Xcode:
- Go to File > Add Package Dependencies...
- Enter the repository URL:
https://github.com/ivan-magda/swiftui-expandable-text.git - Follow the prompts to complete the installation
Usage
Basic Usage
import SwiftUI
import ExpandableText
struct ContentView: View {
var body: some View {
ExpandableText("Lorem ipsum dolor sit amet, consectetur adipiscing elit...")
.padding()
}
}Markdown Support
String literals automatically render markdown:
ExpandableText("This is **bold**, *italic*, and ~~strikethrough~~ text. Visit [Apple](https://apple.com) for more.")For string variables or to disable markdown parsing, use verbatim::
let text = fetchedContent // String variable
ExpandableText(verbatim: text)Customization
ExpandableText(longText)
.font(.body)
.foregroundStyle(.primary)
.lineLimit(3)
.moreButtonText("Show more")
.moreButtonFont(.caption.bold())
.moreButtonForegroundStyle(.blue)
.expandAnimation(.easeOut)Available Modifiers
| Modifier | Description | Default | |----------|-------------|---------| | font(:) | Sets the font for the text | .body | | foregroundStyle(:) | Sets the text color | .primary | | lineLimit(:) | Sets maximum number of lines when collapsed | 3 | | moreButtonText(:) | Text for the "show more" button | "more" | | moreButtonFont(:) | Font for the "show more" button | Same as text font | | moreButtonForegroundStyle(:) | Shape style for the "show more" button | .accentColor | | expandAnimation(_:) | Animation used when expanding/collapsing | .spring |
How It Works
The ExpandableText component works by:
- Measuring both the truncated and full-size text
- Detecting if truncation is necessary
- Applying a gradient mask to create a smooth fade effect
- Adding a "show more" button when text is truncated
- Expanding/collapsing with animation when the button is tapped
Advanced Example
struct BlogPostView: View {
let post: BlogPost
var body: some View {
VStack(alignment: .leading, spacing: 12) {
Text(post.title)
.font(.title)
.bold()
Text("Posted by \(post.author) • \(post.dateFormatted)")
.font(.caption)
.foregroundColor(.secondary)
ExpandableText(verbatim: post.content)
.font(.body)
.foregroundStyle(.primary)
.lineLimit(4)
.moreButtonText("Read more")
.moreButtonFont(.caption.bold())
.moreButtonForegroundStyle(.blue)
.padding(.vertical, 4)
Divider()
}
.padding()
}
}Documentation
Full API documentation available at Swift Package Index.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the project
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
License
This project is licensed under the MIT License - see the LICENSE file for details.
Acknowledgments
- Inspired by the need for a more customizable text expansion solution in SwiftUI
- Thanks to the SwiftUI community for support and feedback
Package Metadata
Repository: ivan-magda/swiftui-expandable-text
Default branch: main
README: README.md