admin管理员组

文章数量:1122832

I have the following view - .swift

    VStack(alignment: .leading, spacing: 0) {
      // some code
      Text(item.text)
        .controlSize(.regular)
        .lineLimit(100)
      // more code
    }
    .frame(maxWidth: 800)

The text wraps finely for multiline content but fails to wrap for single-line strings. Is there any way to properly wrap the text so it spawns on multiple lines?

I've tried multiple combinations of lineLimit, fixedSize, geometry reader, but every solution ended up breaking in some other weird way.

What I am trying to achieve:

What I get instead:

I have the following view - https://github.com/p0deje/Maccy/blob/master/Maccy/Views/PreviewItemView.swift

    VStack(alignment: .leading, spacing: 0) {
      // some code
      Text(item.text)
        .controlSize(.regular)
        .lineLimit(100)
      // more code
    }
    .frame(maxWidth: 800)

The text wraps finely for multiline content but fails to wrap for single-line strings. Is there any way to properly wrap the text so it spawns on multiple lines?

I've tried multiple combinations of lineLimit, fixedSize, geometry reader, but every solution ended up breaking in some other weird way.

What I am trying to achieve:

What I get instead:

Share Improve this question asked Nov 22, 2024 at 14:23 p0dejep0deje 4,0531 gold badge28 silver badges37 bronze badges 0
Add a comment  | 

1 Answer 1

Reset to default 0

I guess this is only a preview issue, since preview does not really care with high priority for your frame with the maxWidth parameter.

So you better try

.frame(width: 800)

at the Preview part instead, it will work.

#Preview {
ContentView()
    .frame(width: 800)
}

If it happens on the popover when running the app, you should also consider giving it a fixed width.

本文标签: swiftuiHow to wrap a text inside a macOS popoverStack Overflow