Rendering a wrapped string identically in NSTextField and NSTextView

I have an NSTextField and an NSTextView. I'd like them to have them render a wrapped string with exactly the same frame. I'm going to be using the NSTextField as a temporary overlay view ontop of the NSTextView, and I don't want the user to notice.


Even though both views use the same NSAttributedString, with the same NSParagraphStyle, the NSTextField subclass puts more spacing between lines than the NSTextView.


Is there some property other than the line spacing/paragraph spacing attributes paragraph style that would affect the way NSTextView or NSTextField layout wrapped lines?


Thanks a bundle.


Replies

This sounds like an exercise in frustration. You don't know (for example) whether the attributes are overriden by the text field, and you don't know if hacks you find now will produce the same text in older or newer versions of macOS.


One approach you could try is to provide a custom field editor for the text field, and try to customized its text view to match your text view.


What's the use case here? Why can't you just use a second text view over the first, or why do you need a second text view at all?

Thanks for the reply Quincey.


Right, I guess the bad assumption was that two text displaying views would draw the same NSAttributedString identically, whereas in actual fact they can draw the strings however they like. Though in the case of NSTextField and NSTextView, they do appear to draw the same except for the line spacing issue.


The use case is creating a temporary overlay view ontop of an NSTextView which allows the user to drag paragraphs around (the other paragraphs move out of the way as you drag, iOS homescreen style; it looks really good). I went for labels to represent each paragraph to be more lightweight and because I don't need any editing, but I suppose I could just use NSTextViews or draw even the attributed strings into images. The user shouldn't be able to detect any difference between the NSTextView and the overlay view for the effect to work.

Another approach you could consider is using text containers (NSTextContainer). You could use a single container normally, but reflow the text into 3 containers for dragging. The trick here would be to control performance, if there is a lot of text involved.


Drawing the text into images has the same problem as using text fields, I think: you can't ensure that the drawing is the same as a text view, without using a text view.