微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

NSTextView.visibleRect与SwiftUI不能正常工作

如何解决NSTextView.visibleRect与SwiftUI不能正常工作

我正在尝试制作类似discord的文本字段。因此,我使用了here中的自定义文本字段并进行了一些修改。 (更改为不绘制背景)但是它没有像我预期的那样花费。而且我发现(NSTextView).visibleRect.height在两行之后没有变化。

Example Animated Image

修改部分:

// ...
        // Appearance
        textView.usesAdaptiveColorMappingForDarkAppearance = true
        textView.font = nsFont
        textView.textColor = NSColor.textColor
        textView.drawsBackground = false
        textView.setContentCompressionResistancePriority(.defaultLow,for: .horizontal)
// ...

MainView和MessageBar:

struct MainView: View {
  var body: some View {
    MessageBar()
      .frame(minWidth: 640,maxWidth: 640,minHeight: 0,maxHeight: .infinity)
  }
}

struct MessageBar: View {

  @Environment(\.colorScheme) var colorScheme
  @State var message = NSAttributedString(string: "")

  var body: some View {
    vstack(spacing: 0) {
      Divider()
      HStack(spacing: 0) {
        Image(systemName: "plus.circle.fill").padding(8.0).padding(.leading,8)
          .font(.title)
        MultilineTextField(NSAttributedString(string: "Message #general"),text: $message,nsFont: NSFont.preferredFont(forTextStyle: .title2)).foregroundColor(colorScheme == .light ? .black : .white)
          .font(.title2)
        Spacer()
        Image(systemName: "gift.fill").padding(8.0)
          .font(.title)
        Image(systemName: "photo.fill").padding(8.0)
          .font(.title)
        Image(systemName: "face.smiling").padding(8.0).padding(.trailing,8)
          .font(.title)
      }.background(colorScheme == .light ? Color.init(.sRGB,red: 235 / 255,green: 237 / 255,blue: 239 / 255,opacity: 1.0) : Color.init(.sRGB,red: 64 / 255,green: 68 / 255,blue: 75 / 255,opacity: 1.0)).cornerRadius(10.0).padding(10)
    }.foregroundColor(.secondary)
  }
}

// Present the view in Playground
PlaygroundPage.current.setLiveView(MainView())

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。