kyle-n / HighlightedTextEditor

A SwiftUI view for dynamically highlighting user input
MIT License
726 stars 69 forks source link

Invalid numeric value #71

Closed lmunck closed 1 year ago

lmunck commented 1 year ago

EDIT: I also get the error with TextField, so this is not related to your package. Apologies for not testing that in advance.


I wanted to avoid HighlightedTextEditor to collapse in a ScrollView, and therefore did the below code to make sure it has a height that SwiftUI would understand.

struct TextBlockView: View {

    @Binding var text:TextBlock

    @State private var height:CGFloat = 10

    var body: some View {
        VStack {

            HighlightedTextEditor(text: $text.plainText, highlightRules: .markdown)
                .introspect { editor in
                    // access underlying UITextView or NSTextView
                    let fixedWidth = editor.textView.frame.size.width
                    let newSize = editor.textView.sizeThatFits(CGSize(width: fixedWidth, height: CGFloat.greatestFiniteMagnitude))

                    editor.textView.backgroundColor = .red
                    editor.textView.isScrollEnabled = false
                    editor.scrollView?.isScrollEnabled = false

                    DispatchQueue.main.async {
                        self.height = newSize.height
                    }

                }
        }.frame(height: height)

    }
}

The view works as expected, but I get the following warning in the console:

[Unknown process name] Error: this application, or a library it uses, has passed an invalid numeric value (NaN, or not-a-number) to CoreGraphics API and this value is being ignored. Please fix this problem.

Not sure if it is a bug or not, but as this approach has worked for other UIViews without issue, I thought I'd share.

kyle-n commented 1 year ago

EDIT: I also get the error with TextField, so this is not related to your package. Apologies for not testing that in advance.

No worries! Probably assigning a variable to be 0 or nil somewhere in the introspect callback.