https://github.com/iaenhaall/attributedtext
AttributedText is a view for displaying some HTML-tagged text using SwiftUI Text View.
https://github.com/iaenhaall/attributedtext
attributed attributed-text easy-to-use formatted formatted-text html-tags swiftui swiftui-text text
Last synced: 12 months ago
JSON representation
AttributedText is a view for displaying some HTML-tagged text using SwiftUI Text View.
- Host: GitHub
- URL: https://github.com/iaenhaall/attributedtext
- Owner: Iaenhaall
- License: mit
- Created: 2021-03-11T20:04:37.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2021-07-09T10:43:41.000Z (over 4 years ago)
- Last Synced: 2025-03-20T10:39:13.043Z (about 1 year ago)
- Topics: attributed, attributed-text, easy-to-use, formatted, formatted-text, html-tags, swiftui, swiftui-text, text
- Language: Swift
- Homepage:
- Size: 355 KB
- Stars: 64
- Watchers: 4
- Forks: 9
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# AttributedText
AttributedText is a view for displaying some HTML-tagged text using **SwiftUI Text View**.
A simple example of usage.
**Code example:**
```swift
AttributedText("A unit of length equal to one hundred-millionth of a centimetre, 10−10 metre, used mainly to express wavelengths and interatomic distances.")
```
**Result:**

## Features
You can clone the repo and run the **AttributedTextExample** project to explore the `AttributedText` features.
These are the main points to pay attention to.
1. You can define the tags you need or use the defaults.
You need to set the required tags and provide associated closures. Each closure must be a modifier that is applied to the **SwiftUI Text View** when a specific tag is encountered.
##### Override the default values, for example at application launch.
```swift
@main
struct ExampleApp: App {
init() {
AttributedText.tags = [
"b": { $0.bold() },
"i": { $0.italic() }
]
}
}
```
In this case only **\** and **\** tags will be processed. All other tags will be ignored or deleted.
##### Set the custom values for each instance.
```swift
private let tags: Dictionary (Text)> = [
// Set the necessary values.
]
var body: some View {
AttributedText("Text", tags: tags)
}
```
2. Basic modifiers can still be applied, such as changing the font and color of the text.
**Code example:**
```swift
AttributedText("This is bold and italic text.")
.foregroundColor(.blue)
.font(.title)
```
**Result:**

3. Handles unopened/unclosed tags.
**Code example:**
```swift
AttributedText("This is italic and bold text.")
```
**Result:**

4. Supports overlapping tags.
**Code example:**
```swift
AttributedText("This is bold only, bold and italic and italic only text.")
```
**Result:**

5. Deletes tags that have no modifiers.
**Code example:**
```swift
AttributedText("This is bold and italic text.")
```
**Result:**

6. Does **not** handle HTML characters such as `&`.
**Code example:**
```swift
AttributedText("This is bold & italic text.")
```
**Result:**

7. **Only single-word tags are supported**. Tags with more than one word or containing any characters besides **letters** or **numbers** are ignored and not removed.
**Code example:**
```swift
AttributedText("This is bold and italic text.")
```
**Result:**

## Installation and usage
#### Via Swift Package Manager
1. In Xcode 11 or greater select `File ▸ Swift Packages ▸ Add Package Dependency`.
2. Paste the link to this repo https://github.com/Iaenhaall/AttributedText.git and click **Next**.
3. Define the package options for this package or select the default. Click **Next**.
4. Xcode downloads the code from GitHub and adds the package to the your project target. Click **Finish**.
#### Manually
1. Add **[AttributedText.swift](https://github.com/Iaenhaall/AttributedText/blob/master/Sources/AttributedText/AttributedText.swift)** and **[HTML2TextParser.swift](https://github.com/Iaenhaall/AttributedText/blob/master/Sources/AttributedText/HTML2TextParser.swift)** files to your project.