在UIKit 框架中,富文本是以创建 NSMutableAttributedString 并添加对应的富文本属性来设置文本样式.

而在SwiftUI 中则是以 Text("") + Text("") 的方式实现.多个文本连接时会自动换行.

例:

Text("注册前请您同意以下协议")

+ Text("<用户隐私协议>").foregroundColor(.red)

+ Text("<用户许可协议>").foregroundColor(.blue)

 

总体来说,方便了许多.

但是目前无法对单个Text设置onTap点击响应.因为onTap会返回view而不是Text.用 as! Text 转换类型虽然满足语法,却无法通过编译.