现在你可以使用这个了
var body: some View {
let nsAttributedString = NSAttributedString(string: "How to use Attributed String in SwiftUI \n How to use Attributed String in SwiftUIHow to use Attributed String in SwiftUI", attributes: [.font: UIFont.systemFont(ofSize: 17), .backgroundColor: UIColor.red])
let attributedString = try! AttributedString(nsAttributedString, including: \.uiKit)
return Text(attributedString)
.multilineTextAlignment(.center)
}
NSAttributedString
来创建一个 AttributedString
。 - HangarRashAttributedString
,而无需使用 NSAttributedString
。甚至 AttributedString 的文档 都会向你展示如何做到这一点。 - HangarRashUIViewRepresentable
为带有attributedString
的UILabel
创建一个。或者,您可以使用AttributedText from SwiftUIX。 Text(attributedString)
构造函数来设置文本的样式:func customText() -> some View {
var attributedString = AttributedString("Recommended: While using the app")
// Adding bold for an example
let range = attributedString.range(of: "Recommended:")!
attributedString[range].font = Font.system(size: 14).bold()
// Adding background color as you want
attributedString.backgroundColor = Color(hex: 0xFFDCBC)
return Text(attributedString)
}