0

After much reading and experimenting, I still cannot get a simple TextView to resize fully in the horizontal direction using Xcode 5.0.2 in Mavericks. It resizes partially as the window is resized, then stops with long lines wrapped around even though my containing NSScrollView continues to resize as expected (it has four default constraints and no horizontal scroller).

Can anyone point me to a simple code/IB+AutoLayout example, preferably just a window containing just an NSTextView dragged in from the IB template library --- one that works? The Apple TextEdit sample code is almost irrelevant for this purpose although it does resize horizontally quite well. Also, there is the clip view for which I can find little information.

Any other tips appreciated.

Thanks.

  • Does the interface builder in Xcode show any problems with the constraints in your window at design time? – Jay Feb 05 '14 at 18:33

2 Answers2

1

Answering my own question:

Turns out that my problem had nothing to do with AutoLayout and little to do with NSTextView. It was the textfile I was using to test my code! This file was composed of records with tab-delimited fields.

Turns out that NSTextView comes with a default NSParagraphStyle with predefined tab stops that end at character 56 whereas my test file had tabs beyond that. Therefore, my lines wrapped around at the last defined tab no matter how much I stretched the window.

After changing my search terms, I found what I needed at the following links:

Premature line wrapping in NSTextView when tabs are used

How to have unlimited tab stops in a NSTextView with disabled text wrap

Apologies for wasting bandwidth.

Community
  • 1
  • 1
0

Not sure why such a simple thing does not work in your case, but nevertheless here's what I did in Xcode to get an NSTextView follow window resize:

  1. Create a new project (not document based in my case but it doesn't really make a difference)
  2. Drag a NSTextView from the palette to your window. Align all four edges with the window edges.
  3. Open the "Add constraints" pop-up (second button from the segmented control on the bottom-right part of your IB view.
  4. Each of the four spacing constraints should show a number equal to the distance of your text view from the container window. If you aligned them, this number should be either 0 or -1. Click the down arrow for each of them and select "Use Current Canvas Value". Do it for all four. Make sure no other constraints are selected.
  5. Click on "Add constraints" on the bottom of the panel.
  6. Run your project. Your textview should resize with the window.

Also, as Jay's comment mentions, make sure you do not have any "leftover" constraints in your view. You can check this either by observing Xcode's warnings, or manually by inspecting your view's constraints by going to the Size Inspector tab (4th tab on the Utilities bar).

If you need to have your textview arranged in a more complex layout, it might be worth taking a look at the AutoLayout Guide.

insys
  • 1,288
  • 13
  • 26