How should the new AttributedString be used with UILabel and other UIKit components? The only way I can find is to use NSAttributedString() to convert it, but this seems to lose much of the formatting (e.g. bold) when I try it in beta 1.
AttributedString with UIKit?
Some UIKit components already support AttributedString
such as button configurations. To apply an AttributedString
to a property that currently only supports NSAttributedString
, you can use the NSAttributedString
conversion initializer. This initializer will create an NSAttributedString
from your AttributedString
including all Foundation, AppKit/UIKit, and SwiftUI attributes (for converting custom attributes, see the other NSAttributedString
conversion initializers). If you're seeing attributes that are part of these scopes lost during conversion using this initializer, please file a request in Feedback Assistant.
Thanks; I've filed FB9148035 for the missing attributes when rendering with UILabel.attributedText = NSAttributedString(text).
One note, though: it's difficult to determine whether the attributes are part of the supported scope as those aren't documented -- I have to browse the Swift definition itself (AttributeScopes.uiKit) to try to determine that. In this case it certainly looks like the lost attributes (bold, paragraph break, unordered list) are meant to be supported.