有没有一种方法可以使用SwiftUI将文本限制为2/3行?
我正在尝试使用有没有一种方法可以使用SwiftUI将文本限制为2/3行?,swiftui,xcode11,ios13,Swiftui,Xcode11,Ios13,我正在尝试使用 var body: some View { Text("This is large text. Is there a way that I can unwrap the large text as discussed").lineLimit(2) } 供参考: 我知道 var body: some View { Text("This is large text. Is there a way that I can unwrap the large text as
var body: some View {
Text("This is large text. Is there a way that I can unwrap the large text as discussed").lineLimit(2)
}
供参考:
我知道
var body: some View {
Text("This is large text. Is there a way that I can unwrap the large text as discussed").lineLimit(nil)
}
它将把文本包装成n行。在文本
元素上调用.lineLimit(3)
。(从技术上讲,它可以在任何视图
上调用,在这种情况下,它将限制该视图中所有文本
元素的行。)
从SwiftUI.View
:
/// Sets the maximum number of lines that text can occupy in this view.
///
/// The line limit applies to all `Text` instances within this view. For
/// example, an `HStack` with multiple pieces of text longer than three
/// lines caps each piece of text to three lines rather than capping the
/// total number of lines across the `HStack`.
///
/// - Parameter number: The line limit. If `nil`, no line limit applies.
/// - Returns: A view that limits the number of lines that `Text` instances
/// display.
public func lineLimit(_ number: Int?) -> Self.Modified<_EnvironmentKeyWritingModifier<Int?>>
///设置文本在此视图中可以占用的最大行数。
///
///行限制适用于此视图中的所有“Text”实例。对于
///例如,“HStack”中有多条文本长度超过三条
///行将每段文本的行数限制为三行,而不是限制文本的行数
///跨越“HStack”的行总数。
///
///-参数编号:行限制。如果为“nil”,则不适用行限制。
///-返回:限制“Text”实例的行数的视图
///展示。
公共函数行限制(uu编号:Int?->自修改
因为.linelimit
暂时无法按预期工作。如果您有标题和子标题等多行文本,您可以简单地将文本元素包装在VStack中,然后使用参数.center
指定VStack multilineTextAlignment修饰符并将文本居中,或者您可以添加.multilineTextAlignment(.center)
直接修改文本元素:
VStack{
Text("This is large text. Is there a way that I can unwrap the large text as discussed")
.font(.title3)
.foregroundColor(.white)
}.padding()
.multilineTextAlignment(.center)