How to convert between NSAttributedString and AttributedString
Table of Contents
AttributedString is a new struct for styling string introduced in iOS 15.
- SwiftUI lacks the support for the old
NSAttributedStringbut fully supports this new type. - On the other hand, UIKit supported
NSAttributedStringbut lacked the support ofAttributedStringin most APIs.
So, it is inevitable that you might need to convert between the two at some point.
In this article, I will show you how to convert NSAttributedString to AttributedString and back.
How to convert NSAttributedString to AttributedString
AttributedString has an initializer that accepts NSAttributedString.
AttributedString.init(_ nsStr: NSAttributedString)
So, you can use NSAttributedString in SwiftUI by converting it to AttributedString before use.
private var content: AttributedString {
let string = "Attributed String"
let attributes: [NSAttributedString.Key : Any] = [
NSAttributedString.Key.foregroundColor: UIColor.systemPink,
NSAttributedString.Key.font: UIFont.boldSystemFont(ofSize: 40),
NSAttributedString.Key.underlineStyle: NSUnderlineStyle.single.rawValue
]
// 1
let nsAttributedString = NSAttributedString(string: string, attributes: attributes)
// 2
let attributedString = AttributedString(nsAttributedString)
return attributedString
}
var body: some View {
// 3
Text(content)
}
1 An NSAttributedString.
2 We create AttributedString out of the existing NSAttributedString.
3 SwiftUI Text can render AttributedString out of the box.
You can easily support sarunw.com by checking out this sponsor.
AI Grammar: Correct grammar, spell check, check punctuation, and parphrase.
How to convert AttributedString to NSAttributedString
NSAttributedString also has an initializer that accepts AttributedString.
convenience NSAttributedString.init(_ attrStr: AttributedString)
You can enjoy the new AttributedString in old UIKit components that don't support it by converting it to NSAttributedString before use.
let string = "Attributed String"
// 1
var attributedString = AttributedString(string)
attributedString.font = .boldSystemFont(ofSize: 40)
attributedString.foregroundColor = .systemPink
attributedString.underlineStyle = NSUnderlineStyle.single
let label = UILabel()
// 2
label.attributedText = NSAttributedString(attributedString)
1 Create an attributed string using a new API.
2 UILabel doesn't support setting the text using AttributedString, but we can easily convert AttributedString to NSAttributedString using a new initializer, NSAttributedString(attributedString).
Read more article about Foundation, AttributedString, NSAttributedString, String, or see all available topic
Enjoy the read?
If you enjoy this article, you can subscribe to the weekly newsletter.
Every Friday, you'll get a quick recap of all articles and tips posted on this site. No strings attached. Unsubscribe anytime.
Feel free to follow me on Twitter and ask your questions related to this post. Thanks for reading and see you next time.
If you enjoy my writing, please check out my Patreon https://www.patreon.com/sarunw and become my supporter. Sharing the article is also greatly appreciated.
Become a patron Buy me a coffee Tweet ShareBetter print for AnyKeyPath in Swift 5.8
Swift 5.8 improve the information that is printed out for a keypath.
How to fix "libobjc.A.dylib is being read from process memory" error
If your app takes forever to run on a device and you get this error in the console log. I will share what works for me in this article.