【问题标题】:SwiftUI a Tag View in the middle of a TextSwiftUI 一个文本中间的标签视图
【发布时间】:2020-07-11 09:13:50
【问题描述】:

我有姓名Max 和文字。我希望文本在左侧的行截断之后(没有空格)。这样的事情在 SwiftUI 中是可能的吗?

照片:

我目前使用的代码是这样的:

struct NotificationUser: View {
    var name: String
    var text: String
    
    var body: some View {
        HStack(alignment: .top) {
            Text(name)
            .bold()
            .padding(.horizontal, 5)
            .padding(.vertical, 2)
            .background(Color(.systemGray6))
            .cornerRadius(5)
            
            Text(text)
        }
    }
}

【问题讨论】:

  • 您希望“has just ...”在 Max 的下方和左侧?

标签: swiftui


【解决方案1】:

属性字符串

您可以从 I explained here 的属性字符串中获得帮助,它可以完全访问您要查找的内容。


更原生的 SwiftUI

另外,你可以使用这个技巧:

var body: some View {
    ZStack(alignment: .topLeading) {
        Text(name)
            .bold()
            .padding(.horizontal, 5)
            .padding(.vertical, 2)
            .background(Color(.systemGray6))
            .cornerRadius(5)

        Group {
            Text(name) // This goes as a frame holder
                .bold()
                .foregroundColor(.clear)

                + Text("  " + text)
        }
        .padding(.horizontal, 5)
        .padding(.vertical, 2)
    }
}

结果

【讨论】:

    【解决方案2】:

    这是我为 iOS 13 及更高版本编写的标签视图。 这是一个带有可删除标签的多行文本字段。

    https://github.com/lijin88121/TagTextField

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-05-06
      • 2020-06-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多