Generate styled SwiftUI Text from strings with XML tags.

Overview

glide

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 <bold>localized</bold> and <italic>styled</italic> string",
    styleGroup: myStyleDefinitions
)

The original idea comes directly from SwiftRichString library by Daniele Margutti on GitHub. 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

glide devices

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. <italicStyle>) is also contained in the localized strings.

// This goes to English Localizable.strings
let englishXML = "%1$@ <italicStyle>%2$@</italicStyle>"

// This goes to Swedish Localizable.strings
let swedishXML = "<italicStyle>%2$@</italicStyle> %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: <a href="http://www.example.com">This is a link</a>

๐ŸŽ† Images (not supported)

It is currently not supported to include Image elements within AttributedString.

Custom XML Attributes (not supported)

For example: <italicStyle myAttribute="something"></italicStyle>

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.

You might also like...
Simple XML Parser implemented in Swift
Simple XML Parser implemented in Swift

Simple XML Parser implemented in Swift What's this? This is a XML parser inspired by SwiftyJSON and SWXMLHash. NSXMLParser in Foundation framework is

Fetch a XML feed and parse it into objects

AlamofireXmlToObjects ๐Ÿšจ This is now a subspec of EVReflection and the code is maintained there. ๐Ÿšจ You can install it as a subspec like this: use_fra

๐Ÿ“„ A Swift DSL for writing type-safe HTML/CSS in SwiftUI way

๐Ÿ“„ swift-web-page (swep) Swep is a Swift DSL for writing type-safe HTML/CSS in SwiftUI way. Table of Contents Motivation Examples Safety Design FAQ In

Validate iOS, Android, and Mac localizations. Find errors in .strings, .stringsdict, and strings.xml files.

Locheck An Xcode and Android localization file validator. Make sure your .strings, .stringsdict, and strings.xml files do not have any errors! What do

swift-highlight a pure-Swift data structure library designed for server applications that need to store a lot of styled text

swift-highlight is a pure-Swift data structure library designed for server applications that need to store a lot of styled text. The Highlight module is memory-efficient and uses slab allocations and small-string optimizations to pack large amounts of styled text into a small amount of memory, while still supporting efficient traversal through the Sequence protocol.

Generate SwiftUI Text or AttributedString from markdown strings with custom style names.
Generate SwiftUI Text or AttributedString from markdown strings with custom style names.

iOS 15.0 / macOS 12.0 / tvOS 15.0 / watchOS 8.0 StyledMarkdown is a mini library that lets you define custom styles in code and use them in your local

SheetyColors is an action sheet styled color picker for iOS
SheetyColors is an action sheet styled color picker for iOS

๐Ÿ“ฑ Based on UIAlertController: The SheetyColors API is based on UIKit's UIAlertController. Simply add buttons to it as you would for any other Action Sheet by defining UIAlertAction instances. Therefore, it nicely integrates with the look & feel of all other native system dialogs. However, you can also chose to use the color picker it self without an action sheet.

An iOS-16-styled slider.

Slyderin An iOS-16-styled slider. Available on iOS 13 and later. Slyderin.Demo.mov How to Use Include it with Swift Package Manager. Add it to your vi

An iOS text field that represents tags, hashtags, tokens in general.
An iOS text field that represents tags, hashtags, tokens in general.

WSTagsField An iOS text field that represents tags, hashtags, tokens in general. Usage let tagsField = WSTagsField() tagsField.layoutMargins = UIEdgeI

Convert text with HTML tags, links, hashtags, mentions into NSAttributedString. Make them clickable with UILabel drop-in replacement.
Convert text with HTML tags, links, hashtags, mentions into NSAttributedString. Make them clickable with UILabel drop-in replacement.

Atributika is an easy and painless way to build NSAttributedString. It is able to detect HTML-like tags, links, phone numbers, hashtags, any regex or

Convert text with HTML tags, links, hashtags, mentions into NSAttributedString. Make them clickable with UILabel drop-in replacement.
Convert text with HTML tags, links, hashtags, mentions into NSAttributedString. Make them clickable with UILabel drop-in replacement.

Atributika is an easy and painless way to build NSAttributedString. It is able to detect HTML-like tags, links, phone numbers, hashtags, any regex or

Convert text with HTML tags, links, hashtags, mentions into NSAttributedString. Make them clickable with UILabel drop-in replacement.
Convert text with HTML tags, links, hashtags, mentions into NSAttributedString. Make them clickable with UILabel drop-in replacement.

Convert text with HTML tags, links, hashtags, mentions into NSAttributedString. Make them clickable with UILabel drop-in replacement.

Useful for showing text or custom view tags in a vertical or horizontal scrollable view and support Autolayout at the same time
Useful for showing text or custom view tags in a vertical or horizontal scrollable view and support Autolayout at the same time

Useful for showing text or custom view tags in a vertical or horizontal scrollable view and support Autolayout at the same time. It is highly customizable that most features of the text tag can be configured.

An iOS text field that represents tags, hashtags, tokens in general.
An iOS text field that represents tags, hashtags, tokens in general.

WSTagsField An iOS text field that represents tags, hashtags, tokens in general. Usage let tagsField = WSTagsField() tagsField.layoutMargins = UIEdgeI

Generate Swift and SwiftUI symbols for localized strings files.
Generate Swift and SwiftUI symbols for localized strings files.

localized-strings-symbols An SPM and Xcode build plugin for creating Swift symbols for localized string keys. What It Does This is a very simple build

JSONHelper - โœŒ Convert anything into anything in one operation; JSON data into class instances, hex strings into UIColor/NSColor, y/n strings to booleans, arrays and dictionaries of these; anything you can make sense of!

JSONHelper Convert anything into anything in one operation; hex strings into UIColor/NSColor, JSON strings into class instances, y/n strings to boolea

The Big List of Naughty Strings is a list of strings which have a high probability of causing issues when used as user-input data.
The Big List of Naughty Strings is a list of strings which have a high probability of causing issues when used as user-input data.

The Big List of Naughty Strings is a list of strings which have a high probability of causing issues when used as user-input data. I have put together

SwiftGen is a tool to automatically generate Swift code for resources of your projects (like images, localised strings, etc), to make them type-safe to use.
SwiftGen is a tool to automatically generate Swift code for resources of your projects (like images, localised strings, etc), to make them type-safe to use.

SwiftGen is a tool to automatically generate Swift code for resources of your projects (like images, localised strings, etc), to make them type-safe to use.

Swift Xid - Xid uses MongoDB Object ID algorighm1 to generate globally unique ids with base32 serialzation to produce shorter strings

Swift Xid - Xid uses MongoDB Object ID algorighm1 to generate globally unique ids with base32 serialzation to produce shorter strings

Releases(0.0.2)
Owner
null
Swift minion for simple and lightweight XML parsing

AEXML Swift minion for simple and lightweight XML parsing I made this for personal use, but feel free to use it or contribute. For more examples check

Marko Tadiฤ‡ 975 Dec 26, 2022
CheatyXML is a Swift framework designed to manage XML easily

CheatyXML CheatyXML is a Swift framework designed to manage XML easily. Requirements iOS 8.0 or later tvOS 9.0 or later Installation Cocoapods If you'

Louis Bodart 24 Mar 31, 2022
The most swifty way to deal with XML data in swift 5.

SwiftyXML SwiftyXML use most swifty way to deal with XML data. Features Infinity subscript dynamicMemberLookup Support (use $ started string to subscr

Kevin 99 Sep 6, 2022
Simple XML parsing in Swift

SWXMLHash SWXMLHash is a relatively simple way to parse XML in Swift. If you're familiar with NSXMLParser, this library is a simple wrapper around it.

David Mohundro 1.3k Jan 3, 2023
Easy XML parsing using Codable protocols in Swift

XMLCoder Encoder & Decoder for XML using Swift's Codable protocols. This package is a fork of the original ShawnMoore/XMLParsing with more features an

Max Desiatov 657 Dec 30, 2022
A simple way to map XML to Objects written in Swift

XMLMapper XMLMapper is a framework written in Swift that makes it easy for you to convert your model objects (classes and structs) to and from XML. Ex

Giorgos Charitakis 109 Jan 6, 2023
A fast & lightweight XML & HTML parser in Swift with XPath & CSS support

Fuzi (ๆ–งๅญ) A fast & lightweight XML/HTML parser in Swift that makes your life easier. [Documentation] Fuzi is based on a Swift port of Mattt Thompson's

Ce Zheng 994 Jan 2, 2023
Ji (ๆˆŸ) is an XML/HTML parser for Swift

Ji ๆˆŸ Ji (ๆˆŸ) is a Swift wrapper on libxml2 for parsing XML/HTML. Features Build XML/HTML Tree and Navigate. XPath Query Supported. Comprehensive Unit T

HongHao Zhang 824 Dec 15, 2022
Kanna(้‰‹) is an XML/HTML parser for Swift.

Kanna(้‰‹) Kanna(้‰‹) is an XML/HTML parser for cross-platform(macOS, iOS, tvOS, watchOS and Linux!). It was inspired by Nokogiri(้‹ธ). โ„น๏ธ Documentation Fea

Atsushi Kiwaki 2.3k Dec 31, 2022
A sensible way to deal with XML & HTML for iOS & macOS

Ono (ๆ–ง) Foundation lacks a convenient, cross-platform way to work with HTML and XML. NSXMLParser is an event-driven, SAX-style API that can be cumbers

Mattt 2.6k Dec 14, 2022