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 (将#修改为@)