1

I'm trying to make a Discord-like text field. So I used a custom text field from here and modified a bit. (Changed not to draw background) But it doesn't expend as I expected. And I found that (NSTextView).visibleRect.height is not changing after two lines.

Example Animated Image

Modified part:

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

MainView and 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())
Helloyunho
  • 23
  • 2
  • 7

0 Answers0