The new Swift Attributed String looks really neat, but is there a way to use it in UIKit or is it SwiftUI only? UITextViews take an NSAttributedString for their content, so I didn't know if there was a way I was missing for converting the AttributedString into an NSAttributedString.
NSAttributedString
is given a new initializer taking an AttributedString
.
convenience init(_ attrStr: AttributedString)
You can easily convert an AttributedString
to an NSAttributedString
:
var astr = AttributedString("Hello, world!")
if let range = astr.range(of: "world") {
astr[range].font = .boldSystemFont(ofSize: 22)
}
label.attributedText = NSAttributedString(astr)