Use AttributedString in SwiftUI Before iOS 15

Surely Nobody Needs To?

Steven Curtis
3 min readMay 29, 2023
Photo by Amie Bell on Unsplash

The Problem

Before iOS 15 AttributedString for SwiftUI wasn’t a thing. Something which might produce the following result:

If you wish to have a String with text (perhaps part of the text has colour and part has a size) you might need to come up with a solution. Such a String might be:

var message: AttributedString {
var redString = AttributedString("Is this red?")
redString.font = .systemFont(ofSize: 18)
redString.foregroundColor = .red
var largeString = AttributedString("This should be larger!")
largeString.font = .systemFont(ofSize: 24)
return redString + largeString
}

How hard can it be?

In iOS 15 we would have:

Text(message)

But that initializer isn’t available. What should we do? We would probably have the following:

HStack {
Text("Is this red?")
.font(.system(size: 18))
.foregroundColor(.red)
Text("This should be larger!")
.font(.system(size: 24))
}

--

--