AttributedString, a better way to handle attributed strings in iOS 15

Jesus Guerra
2 min readSep 1, 2022

Are you targeting iOS 15 or higher? I would like to suggest the usage of AttributedString

Photo by Bozhin Karaivanov on Unsplash

As you might know, attributed strings are character strings that have attributes for individual characters or ranges of characters. Attributes provide traits like visual styles for display, accessibility for guided access, and hyperlink data for linking between data sources.

Pretty sure if you had the need to create an attributed string in the past you might encounter with NSAttributedString , and although it works great its usage is not that “neat”. Well, if you are targeting iOS 15 or higher I have a suggestion for you, use AttributedString instead.

What is AttributedString?

I personally think that AttributedString is the next step in the evolution for NSAttributedString

I would like to say AttributedString is the new way to handle attributed strings. AttributedString works in a more verbose form and uses subscripts and dynamic members that can be accessed with dot syntax.

The only “downside” is that it is available on iOS 15 or later.

How to use AttributedString?

Basic Example:

Code:

Result:

Range

Code:

Result:

Markdown

Code:

Result:

Conclusion

I definitely think that AttributedString might be a better way to handle attributed strings in general. Due to the fact that the release of iOS 16 is around the corner, the constraint that it is available on iOS 15 or later might be less and less significant in the further months.

Source

People don’t care about what you say, they care about what you build.

— Mark Zuckerberg

Thanks for reading and I really hope you benefit from this article. As usual, any feedback or comments are welcome.

--

--