Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何在SwiftUI中为单行文本设置行高?_Ios_Swift_Iphone_Xcode_Swiftui - Fatal编程技术网

Ios 如何在SwiftUI中为单行文本设置行高?

Ios 如何在SwiftUI中为单行文本设置行高?,ios,swift,iphone,xcode,swiftui,Ios,Swift,Iphone,Xcode,Swiftui,目前,我一直在使用.linespace(…),但此仅适用于多行文本 /// 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 lineSpac

目前,我一直在使用
.linespace(…)
,但此仅适用于多行文本

/// 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
这意味着,我很难准确地从草图/图中翻译字体,我需要利用填充来正确翻译。以下是一个示例,说明了这一点:

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)
}

您只需使用.frame修改器并设置高度。

Xcode 12

使用
.leading()
修改器调整行距

Text("Hello\nworld").font(Font.body.leading(.tight))
当前支持的值:
.tight
.standard
.loose


来源:

为了让你的文本与figma匹配,我终于找到了一个方法。例如,如果您的figma设计的特定字体为16磅,线条高度为32磅:

let font = UIFont(name: "SomeFont", size: 16)!
return Text("Some Text")
    .font(.custom("SomeFont", size: 16))
    .lineSpacing(32 - font.lineHeight)
    .padding(.vertical, (32 - font.lineHeight) / 2)

为了获得准确的行距值,我们必须用字体的固有行距减去所需的行距,但正如您所注意到的,这仅对多行文字有效,并且仅在行间有效。我们仍然需要考虑文本的顶部和底部填充,以匹配所需的行高,因此再次添加总行高减去字体的行高。

我已成功使用以下组合:

  • .frame(最小高度:线宽)
  • .linespace(abs(designSystemFont.lineHeight-uiKitFont.lineHeight))
可能不需要
abs
(假设系统的线条高度高于默认高度,但您永远不知道这是否会改变,所以…)

这使我能够在遵守设计系统要求的同时处理单行文本。

根据我的回答,我自己做了一个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))
    }
}

实际上,对我有效的是(基于Ole Kristian的回答)

导入快捷界面
结构FontWithLineHeight:ViewModifier{
字体:UIFont
让线宽:CGFloat
func正文(内容:内容)->某些视图{
内容
.font(字体(font))
.linespace((token.lineHeight-token.font.lineHeight)/2)
.padding(.vertical,(token.lineHeight-token.font.lineHeight))
}
}
扩展视图{
func-fontWithLineHeight(字体:UIFont,线宽:CGFloat)->一些视图{
修改内容(内容:self,修改器:FontWithLineHeight(字体:font,线宽:lineHeight))
}
}
…与其他答案非常相似,填充值和行距值颠倒


这是我唯一能让底部“填充”与Figma相同的方法。

我不明白你的意思,但是,如果对你有用,试试这个:在一行的末尾添加“\n”
Text
你对第一个文本的期望是什么?字体与主题的关系如何?两个文本块中的字体是相同的?@Eneskarosman我不想添加新行,我想增加单行的间距。我无法说服自己,单行应该如何具有间距<代码>行距方法名称很清楚,您也访问了SwiftUI的界面来阅读说明,还说了“设置行距…”(SwiftUI文本不提供线宽属性(行距是另一种类型)。您可以尝试对齐“firstTextBaseLine”以获得所需的行为。或者,将“UILabel”(通过“UIViewRepresentable”)与属性字符串一起使用(在段落样式中指定行高)。我宁愿避免硬编码的帧大小,因为它不会随着字体、布局或内容的变化而自动调整。这并不能回答我的问题,但仍然很酷,你可以这样做!也许坚持这三个选项中的任何一个都比从设计师那里获得实际值要好,但有时,设计就是设计,所以我们可以“t默认为应用程序标准。很好,将其放在视图修饰符中会很好;)可能最好将其更新为使用
Font
?或者无法从SwiftUI字体访问
线宽
?正确,字体没有线宽,因此我必须使用UIFont。我只是懒得使用
UIFont
初始值设定项;如果我有时间的话,我会发布一个更新,在一个电话里就可以看到尺寸、重量和行间距。有没有人想出一个解决负
。行间距
?似乎如果你传入一个负数,它将被视为0。