是否有一种 SwiftUI 方式来制作文本上标或下标? [英] Is there a SwiftUI way of making text superscript or subscript?
问题描述
我知道可以像 Text("TM").offset(x:0 y:7)
一样应用偏移量,但是是否有不同或更好的方法来创建一些上标或下标SwiftUI 中的文本?
I know an offset can be applied like Text("TM").offset(x:0 y:7)
but is there a different or better way to create a bit of super or subscript text in SwiftUI?
推荐答案
这是使用 .baselineOffset
的通用方法:
Here's a generic way using .baselineOffset
:
Text("Company")
.font(.callout)
+ Text("TM")
.font(.system(size: 8.0))
.baselineOffset(6.0)
我确信有一种方法可以使用 CTFont
动态获得正确的偏移量,但我认为这可能会很痛苦.一种草率的方法是包装文本视图?在 GeometryReader 中,并使用高度尝试定位它,使其看起来不错.
I'm sure there's a way to get the correct offset dynamically using CTFont
, but I think it may be a pain. A sloppy way would be to wrap the Text View(s)? in a GeometryReader, and use the height to try and position it so it looks good.
您还可以将 Unicode 用于某些符号(例如 ™):
You can also use Unicode for some symbols (like ™):
Text("Company\u{2122}")
这篇关于是否有一种 SwiftUI 方式来制作文本上标或下标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!