Question

I'm having problems understanding how to get an NSTextView to limit the width of the page when displayed and printed. Essentially, what I'm looking to achieve is that once the window containing the text view is resized past a certain point, the text view will "wrap" its contents. TextEdit.app does essentially what I'm looking to achieve:

textedit

However, with my application, I don't get the behaviour at all, especially in relation to images embedded within the text view. For example, when I embed a large image, the image will simply be cut off if the window is too small, but when the window is resized large enough, it will render in its full size, but the text view does not constrain the content to the width of a page:

text view small text view large

This, of course, results in some strange results when printing—for example, the image is simply cut off:

image cut off print

When resizing the window, it's immediately apparent (with both centred, right-aligned or justified text) that the text view isn't constraining its width to a certain number of centimetres, as indicated on the ruler.

How would I go about limiting the width of the NSTextView when printed? I don't care about margins or rendering it as multiple pages in the UI (although I would not mind it,) but want to make sure the content of the text view is no more than the page's width when printing, according to the size set in the Page Setup panel. (To clarify: the user interface can have the text view as wide as it wants, I just require that when printed, it's horizontally fit to a page so nothing is cut off.)

I have looked throughout the documentation, and tried several things, such as NSText's maxSize value, but to no avail.

Was it helpful?

Solution

I fixed this by looking at how Apple actually does it with TextEdit, as its source is available to developers. Specifically, the PrintingTextView.m file was what I was looking at, and it's reasonably easy to integrate into projects.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top