随着动态类型的增加,保持不同尺寸的文本在顶部保持一致
我正在尝试将顶部不同尺寸的文本对齐,以便当动态类型大小变化时它们保持对齐。
import SwiftUI
struct ContentView: View {
var body: some View {
HStack(alignment: .center, spacing: 0) {
Text("$")
.font(Font.custom("Helvetica", size: 24, relativeTo: .footnote))
.baselineOffset(7)
Text("123")
.font(Font.custom("Helvetica", size: 36, relativeTo: .footnote))
Text("45")
.font(Font.custom("Helvetica", size: 24, relativeTo: .footnote))
.baselineOffset(7)
}
}
}
这是在顶部对齐的 常规尺寸
,但随着动态类型的变化而失去顶部对齐:动态类型大小extraix extraimextraextralarge
关于如何使它们保持在顶部的任何想法,即使动态类型大小会更改?
I'm trying to align texts of different sizes at the top so that they stay aligned when the Dynamic Type size changes.
import SwiftUI
struct ContentView: View {
var body: some View {
HStack(alignment: .center, spacing: 0) {
Text("quot;)
.font(Font.custom("Helvetica", size: 24, relativeTo: .footnote))
.baselineOffset(7)
Text("123")
.font(Font.custom("Helvetica", size: 36, relativeTo: .footnote))
Text("45")
.font(Font.custom("Helvetica", size: 24, relativeTo: .footnote))
.baselineOffset(7)
}
}
}
This is aligned at top at
Regular Size
But it loses the top alignment as the Dynamic Type gets bigger: Dynamic Type Size ExtraExtraExtraLarge
Any ideas on how to keep them aligned at top even as the Dynamic Type size changes?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我从来不需要这种配置,但是您的问题引起了我的好奇心。
I never needed this kind of configuration but your question arose my curiosity. ????
(I keep the same context you provided: 3 Text elements with the same text styles/font and two of them having the same font size)
I took a look at the font metrics to understand the returned values.
If these elements are aligned on top in a HStack view, it's the top edge that's concerned, not the letters nor the numbers inside.
So, I have to move vertically the biggest one to make its top content aligned to the smallest one.
To reach this goal, I used the alignmentGuide method that returns
a view modified with respect to its horizontal alignment according to the computation performed in the method's closure
by using theascender
andcapHeight
metrics difference.The initial font sizes can be adapted to the user's preferences by using the dynamic property wrapper
@ScaledMetrics
that scales a numeric value.????I implement this information with Xcode 13.4 and iOS 15:????
Following this rationale, I get the following result to keep texts of different sizes aligned at top as the dynamic type size changes. ????????????

Don't hesitate to adapt this code snippet that fits the example you provided and that's definitely not generic. ????