如何在 SwiftUI 中使用属性字符串

编程入门 行业动态 更新时间:2024-10-25 08:19:03
本文介绍了如何在 SwiftUI 中使用属性字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

如何在 SwiftUI 中使用 AttributedString.没有可用的 API在文本中使用 AttributedString

How to use AttributedString in SwiftUI. There is no API available to use AttributedString in Text

推荐答案

iOS 15 和 Swift 5.5

Text 现在支持 Markdown,您也可以创建自定义属性:

iOS 15 and Swift 5.5

Text now supports markdown and also you can create custom attributes:

您甚至可以远程获取定义的属性,例如:

You can even get defined attributes remotely like:

您可以将多个 Text 对象与一个简单的 + 运算符组合在一起,这将处理一些属性:

You can combine multiple Text objects together with a simple + operator and that will handle some of the attributions:

每个都可以有多个特定的修饰符

由于它不直接支持 Text(直到 iOS 15),您可以将 UILabel 放在那里并随意修改它:

Since it doesn't support directly on Text (till iOS 15), you can bring the UILabel there and modify it in anyway you like:

struct UIKLabel: UIViewRepresentable { typealias TheUIView = UILabel fileprivate var configuration = { (view: TheUIView) in } func makeUIView(context: UIViewRepresentableContext<Self>) -> TheUIView { TheUIView() } func updateUIView(_ uiView: TheUIView, context: UIViewRepresentableContext<Self>) { configuration(uiView) } }

用法:

var body: some View { UIKLabel { $0.attributedText = NSAttributedString(string: "HelloWorld") } }

更多推荐

如何在 SwiftUI 中使用属性字符串

本文发布于:2023-10-27 02:54:01,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1532095.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   属性   如何在   SwiftUI

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!