iaenhaall/attributedtext
AttributedText is a view for displaying some HTML-tagged text using **SwiftUI Text View**.
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.
- 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 \<b\> and \<i\> tags will be processed. All other tags will be ignored or deleted.
##### Set the custom values for each instance.
```swift private let tags: Dictionary<String, (Text) -> (Text)> = [ // Set the necessary values. ]
var body: some View { AttributedText("Text", tags: tags) } ```
- Basic modifiers can still be applied, such as changing the font and color of the text.
Code example:
``swift AttributedText("This is <b>bold</b> and <i>italic</i> text.") .foregroundColor(.blue) .font(.title) ``
Result:
[1 feature]
- Handles unopened/unclosed tags.
Code example:
``swift AttributedText("This is italic</i> and <b>bold text.") ``
Result:
[2 feature]
- Supports overlapping tags.
Code example:
``swift AttributedText("This is <b>bold only, <i>bold and italic</b> and italic only</i> text.") ``
Result:
[3 feature]
- Deletes tags that have no modifiers.
Code example:
``swift AttributedText("<unknown>This is <b>bold</b> and <i>italic</i> text.</unknown>") ``
Result:
[4 feature]
- Does not handle HTML characters such as
&.
Code example:
``swift AttributedText("This is <b>bold</b> & <i>italic</i> text.") ``
Result:
[5 feature]
- 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 <tag attribute1=\"value1\"> <b>bold</b> and <i>italic</i> text</tag>.") ``
Result:
[6 feature]
Installation and usage
Via Swift Package Manager
- In Xcode 11 or greater select
File ▸ Swift Packages ▸ Add Package Dependency. - Paste the link to this repo https://github.com/Iaenhaall/AttributedText.git and click Next.
- Define the package options for this package or select the default. Click Next.
- Xcode downloads the code from GitHub and adds the package to the your project target. Click Finish.
Manually
- Add AttributedText.swift and HTML2TextParser.swift files to your project.
Package Metadata
Repository: iaenhaall/attributedtext
Default branch: master
README: README.md