Friday, May 9, 2025
Alternative Way
  • Home
  • Latest
    • Latest
  • News
  • World Tech
  • World Gaming
  • Minecraft
  • Guides
  • Contact Us
  • About The Team
    • Privacy Policy
    • Terms of Use
No Result
View All Result
  • Home
  • Latest
    • Latest
  • News
  • World Tech
  • World Gaming
  • Minecraft
  • Guides
  • Contact Us
  • About The Team
    • Privacy Policy
    • Terms of Use
No Result
View All Result
Alternative Way
No Result
View All Result

Generate styled SwiftUI Text from strings with XML tags

Gordon James by Gordon James
October 3, 2021
in World Tech Code
0 0
0
Home World Tech Code

XML tags are often used to generate styled text on web pages. For example, cascading style sheets (CSS) are used to style webpages in order to make them look pretty and provide a consistent look and feel. CSS is used by a web designer to style webpages in such a way that they are consistent and look good. But since the web is evolving very quickly, we are stuck with the old way of styling webpages, which is to use XML tags like div or p to specify which CSS styling rules to apply to a given element.

In this article, I’ll teach you to generate styled text from XML tags. You can create your own custom text styles to freely adjust your UI’s font styles. You can even create your own XML tags to generate your own custom text styles.

While working with SwiftUI text elements, it’s sometimes necessary to generate a text string from the element content. You can accomplish this using the UILabel class’s text property, which generates a string based on the label’s current contents. However, if the content is in an XML tag, the resulting string won’t be styled consistently across the system.

Table of Contents

Toggle
  • XMLText
  • Examples
  • Text Modifiers Supported
      • font(SwiftUI.Font)
      • Foreground color(Colour)
      • crossed out (colour)
      • Underscore (color)
      • Crenation(CGFloat)
      • follow-up(CGFloat)
      • BaselineOffset(CGFloat)
  • Duty model
    • References (not supported)
    • Images (not supported)
    • XML custom attributes (not supported)
  • GitHub
      • Related Tags:

XMLText

XMLText is a mini library that generates SwiftUI text from a given XML tagged string. It uses the + text operator to compose the final result. Text( xmlString : my localized and formatted string, styleGroup : myStyleDefinitions ) The original idea came directly from Daniele Margutti’s SwiftRichString library on GitHub. The code to parse XML, StyleProtocol and StyleGroup is taken from this library, with minor modifications to make SwiftUI Text instead of NSAttributedString. This is very useful for locating styled string applications without having to know the location of the strings in the code to be styled. This is a pretty good alternative to using NSAttributedString with UIViewRepresentable UILabel in a SwiftUI application, since the UIViewRepresentable markup for dynamic views like UILabel doesn’t always work and is prone to errors when combined with other SwiftUI views.

Examples

Generate styled SwiftUI Text from strings with XML tags iOS 14.0 / macOS 11.0 / tvOS 14.0

Text Modifiers Supported

font(SwiftUI.Font)

Foreground color(Colour)

crossed out (colour)

Underscore (color)

Crenation(CGFloat)

follow-up(CGFloat)

BaselineOffset(CGFloat)

Duty model

Here is an example of the XML strings that will appear in your Localizable.strings files, with the words for each language in a different order, i.e. English and Swedish in this example. If you are not familiar with this approach, note that style information (such as StyleGroup keys) is also contained in localized strings. // This goes to English Localizable.strings let englishXML = %[email protected] %[email protected] // This goes into the Swedish Localizable.strings lets swedishXML = %[email protected] %[email protected] 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, Director, Martin), styleGroup : styleGroup )

References (not supported)

Currently, SwiftUI does not allow other display elements (e.g., a button) to be merged with text elements using the + operator. Adding gesture recognition to pieces of text using the + operator is also not supported because gesture recognition modifiers return an opaque display type, meaning it is no longer text and cannot be added to other text. If you have only one reference in a particular paragraph or sentence, you can avoid using touch screen recognition on the entire paragraph or sentence generated by the XMLText library. If you have multiple references in one sentence or paragraph, good luck with NSAttributedString and UIViewRepresentable from UITextView. ♂️

Images (not supported)

As for links, SwiftUI does not currently allow image and text elements to be combined using the + operator.

XML custom attributes (not supported)

For example: This feature is not currently supported, for reasons of simplicity and because the library does not have enough functionality to make it worthwhile. If use cases arise in this context, an approach similar to the XMLDynamicAttributesResolver in the SwiftRichString library may be considered in the future.

GitHub

https://github.com/cocoatoucher/XMLTextText in your app can be a pain if you don’t have an elegant way to display it. That’s why I created a framework called SwiftUI, which makes it easy to generate styled text from your strings, and has been used in several apps including the iOS and Android versions of the blog.. Read more about nsattributedstring and let us know what you think.

Related Tags:

swiftui attributed string linkswiftui text to stringswiftui attributed string htmlnsattributedstringswiftui html textswift bold text in string,People also search for,Privacy settings,How Search works,swiftui attributed string link,swiftui text to string,swiftui attributed string html,nsattributedstring,swiftui html text,swift bold text in string,change font of attributed string: swift,nsattributedstring builder

Total
0
Shares
Share 0
Tweet 0
Pin it 0
Share 0
ShareTweet
Gordon James

Gordon James

Next Post
🥇 +15 Alternatives to Facebook Live  List ▷ 2021

🥇 +15 Alternatives to Facebook Live  List ▷ 2021

  • Trending
  • Comments
  • Latest
How To Get Free Internet On Android Without Service

How To Get Free Internet On Android Without Service

March 10, 2022
🥇 +4 Neo Geo Emulators for Android  List ▷ 2021

🥇 +4 Neo Geo Emulators for Android  List ▷ 2021

October 3, 2021

Fix: Notifications not working on Nova Launcher

October 3, 2021
How to Fix OpenVPN Connected but Not Changing IP Address

How to Fix OpenVPN Connected but Not Changing IP Address

October 3, 2021

Setting Up Directory Sync Between On-Premises Active Directory with Microsoft 365 Azure AD

0
🥇 DELETE ACCOUNT from PS4  ▷ Step by Step Guide ▷ 2020

🥇 DELETE ACCOUNT from PS4  ▷ Step by Step Guide ▷ 2020

0
🥇 PPTX File Extension  What is .Pptx and how to open them? ▷ 2020

🥇 PPTX File Extension  What is .Pptx and how to open them? ▷ 2020

0
🥇 Make a Crossword in Microsoft Word  Step by Step Guide ▷ 2020

🥇 Make a Crossword in Microsoft Word  Step by Step Guide ▷ 2020

0
What to Know About Car Shipping Services

What to Know About Car Shipping Services

May 7, 2025
CS2 Skins-Why Trade Them?

CS2 Skins-Why Trade Them?

May 7, 2025
Alternative Routes: Successfully Exiting Your Wyndham Timeshare Without The Stress

Alternative Routes: Successfully Exiting Your Wyndham Timeshare Without The Stress

May 6, 2025
The Ultimate Seiko Watch Gift Guide

The Ultimate Seiko Watch Gift Guide

May 1, 2025

There's always an alternative Way!
Find us at 4145 Zolynthian Street, Vylorthos, QP 78425
No Result
View All Result
  • Home
  • Latest
    • Latest
  • News
  • World Tech
  • World Gaming
  • Minecraft
  • Guides
  • Contact Us
  • About The Team
    • Privacy Policy
    • Terms of Use

© 2022 - Alternative Way

Welcome Back!

Login to your account below

Forgotten Password?

Retrieve your password

Please enter your username or email address to reset your password.

Log In
We use cookies on our website to give you the most relevant experience by remembering your preferences and repeat visits. By clicking “Accept All”, you consent to the use of ALL the cookies. However, you may visit "Cookie Settings" to provide a controlled consent.
Cookie SettingsAccept All
Manage consent

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. These cookies ensure basic functionalities and security features of the website, anonymously.
CookieDurationDescription
cookielawinfo-checkbox-analytics11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Analytics".
cookielawinfo-checkbox-functional11 monthsThe cookie is set by GDPR cookie consent to record the user consent for the cookies in the category "Functional".
cookielawinfo-checkbox-necessary11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookies is used to store the user consent for the cookies in the category "Necessary".
cookielawinfo-checkbox-others11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Other.
cookielawinfo-checkbox-performance11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Performance".
viewed_cookie_policy11 monthsThe cookie is set by the GDPR Cookie Consent plugin and is used to store whether or not user has consented to the use of cookies. It does not store any personal data.
Functional
Functional cookies help to perform certain functionalities like sharing the content of the website on social media platforms, collect feedbacks, and other third-party features.
Performance
Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.
Analytics
Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics the number of visitors, bounce rate, traffic source, etc.
Advertisement
Advertisement cookies are used to provide visitors with relevant ads and marketing campaigns. These cookies track visitors across websites and collect information to provide customized ads.
Others
Other uncategorized cookies are those that are being analyzed and have not been classified into a category as yet.
SAVE & ACCEPT
No Result
View All Result
  • Home
    • Home – Layout 1
    • Home – Layout 2
    • Home – Layout 3
    • Home – Layout 4
    • Home – Layout 5
  • Travel News

© 2025 JNews - Premium WordPress news & magazine theme by Jegtheme.