https://github.com/cocoatoucher/xmltext
Generate styled SwiftUI Text from strings with XML tags.
https://github.com/cocoatoucher/xmltext
attributed attributedstring html ios localisation localization macos parsing string style styled swift swiftui text tvos xml
Last synced: 11 days ago
JSON representation
Generate styled SwiftUI Text from strings with XML tags.
- Host: GitHub
- URL: https://github.com/cocoatoucher/xmltext
- Owner: cocoatoucher
- License: mit
- Created: 2021-03-10T21:00:29.000Z (about 4 years ago)
- Default Branch: main
- Last Pushed: 2022-01-04T07:53:09.000Z (over 3 years ago)
- Last Synced: 2025-03-29T20:51:09.254Z (about 1 month ago)
- Topics: attributed, attributedstring, html, ios, localisation, localization, macos, parsing, string, style, styled, swift, swiftui, text, tvos, xml
- Language: Swift
- Homepage:
- Size: 539 KB
- Stars: 21
- Watchers: 6
- Forks: 6
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
![]()
XMLText is a mini library that can generate SwiftUI `Text` from a given XML string with tags. It uses `AttributedString` to compose the final text output.
```
Text(
xmlString: "my localized and styled string",
styleGroup: myStyleDefinitions
)
```***The original idea comes directly from [`SwiftRichString` library by `Daniele Margutti` on GitHub](https://github.com/malcommac/SwiftRichString). Code for XML parsing, StyleProtocol, and StyleGroup are taken from this library, slight modifications are made to them in order to generate `SwiftUI` `Text` instead of `NSAttributedString`.***
This is really useful for localising your apps for styled strings without having to know the location of the strings in the code that needs to be styled. This is a pretty fine alternative to having to use `NSAttributedString` with `UIViewRepresentable` of a `UILabel` in a `SwiftUI` app, as the layout of `UIViewRepresentable` for such dynamic views as `UILabel` doesn't always work and is prone to glitches when combined with other `SwiftUI` views.
## Examples
![]()
iOS 15.0 / macOS 12.0 / tvOS 15.0 / watchOS 8.0## Supported `Text` modifiers
#### font(*SwiftUI.Font*)
#### foregroundColor(*Color*)
#### strikethrough(*Color*)
#### underline(*Color*)
#### kerning(*CGFloat*)
#### tracking(*CGFloat*)
#### baselineOffset(*CGFloat*)## Sample usage
This is an example of XML strings that would appear in your Localizable.strings files with words in different order for each different language, namely English and Swedish for this example.
If you are not familiar with that approach, please note that the style information(`StyleGroup` keys, e.g. ``) is also contained in the localized strings.```
// This goes to English Localizable.strings
let englishXML = "%1$@ %2$@"// This goes to Swedish Localizable.strings
let swedishXML = "%2$@ %1$@"let normalStyle = Style { style in
style.font = .subheadline
style.foregroundColor = .red
}let italicStyle = Style { style in
style.font = Font.italic(.system(size: 20))()
style.foregroundColor = .blue
}let styleGroup = StyleGroup(
base: normalStyle,
["italicStyle": italicStyle]
)Text(
xmlString: String(format: englishXML, "Director", "Martin"),
styleGroup: styleGroup
)
Text(
xmlString: String(format: swedishXML, "Regissör", "Martin"),
styleGroup: styleGroup
)
```### 🔗 Links
You can add links inside your strings via:
`This is a link`### 🎆 Images (not supported)
It is currently not supported to include `Image` elements within `AttributedString`.
### Custom XML Attributes (not supported)
For example: ``
This is currently not supported for sake of simplicity and given the fact that the library doesn't have so many capabilities for that to make sense. If there would be some use cases regarding this, a similar approach to `XMLDynamicAttributesResolver` of `SwiftRichString` library could be considered in the future.