【发布时间】:2020-07-23 08:09:25
【问题描述】:
我有一个要点和一些长的多行文本。我希望项目符号点与第一行文本的中心对齐。显然,如果字符串足够短且一行长,那么两个视图会自动居中对齐。我对文本长度超过一行的情况感兴趣。
var body: some View {
HStack {
Image(systemName: "circle.fill")
.font(.system(size: 8))
Text("Insert really long multiline string that wraps.")
}
}
这可能吗?
更新 1:
将 HStack 对齐设置为 top 将图像的顶部与文本的顶部对齐,像这样...
var body: some View {
HStack(alignment: .top) {
Image(systemName: "circle.fill")
.font(.system(size: 8))
Text("This is really really really really really really really really really really really really really really really really really really really really really really really really long string.")
}
}
更新 2:
我能想到的唯一选择是this,除了这是 UIKit...
// Aligns the icon to the center of a capital letter in the first line
let offset = label.font.capHeight / 2.0
// Aligns the icon to the center of the whole line, which is different
// than above. Especially with big fonts this makes a visible difference.
let offset = (label.font.ascender + label.font.descender) / 2.0
let constraints: [NSLayoutConstraint] = [
imageView.centerYAnchor.constraint(equalTo: label.firstBaselineAnchor, constant: -offset),
imageView.trailingAnchor.constraint(equalTo: label.leadingAnchor, constant: -10)
]
NSLayoutConstraint.activate(constraints)
【问题讨论】:
标签: swiftui