目前,我一直在使用.lineSpacing(...)
, 但是这个仅适用于多行文本.
/// Sets the amount of space between lines of text in this view.
///
/// - Parameter lineSpacing: The amount of space between the bottom of one
/// line and the top of the next line.
@inlinable public func lineSpacing(_ lineSpacing: CGFloat) -> some View
这意味着我很难准确地从 sketch/figma 翻译字体,并且我需要调整填充以使其正确。下面的示例说明了这一点:
VStack {
// Line spacing is ignored.
Text("Hello, World!")
.background(Color.green)
.lineSpacing(50)
Spacer()
// Line spacing is correct.
Text("Lorem ipsum is placeholder text commonly used in the graphic, print, and publishing industries for previewing layouts and visual mockups.")
.background(Color.green)
.lineSpacing(50)
}
基于答案丹·哈桑 https://stackoverflow.com/users/13835223/dan-hassan我为自己制作了一个 ViewModifier 来执行此操作,看起来它按预期工作
import SwiftUI
struct FontWithLineHeight: ViewModifier {
let font: UIFont
let lineHeight: CGFloat
func body(content: Content) -> some View {
content
.font(Font(font))
.lineSpacing(lineHeight - font.lineHeight)
.padding(.vertical, (lineHeight - font.lineHeight) / 2)
}
}
extension View {
func fontWithLineHeight(font: UIFont, lineHeight: CGFloat) -> some View {
ModifiedContent(content: self, modifier: FontWithLineHeight(font: font, lineHeight: lineHeight))
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)