1

There are several places on the net ( including stackoverflow ) pointing out how to disable text wrapping on a NSTextView object. The method works but there is a problem with the tab stops. It seems that the default is 12 tab stops, using more than 12 will start wrapping. The obvious solution would be to change the default NSParagraphStyle so that it has more tab stops, but I dont think this is the proper way to do it. Ideally I would like to have "infinity" amount of tab stops, as for example xcode has.

Is there any straight forward way to achieve this?

Manuel Astudillo
  • 186
  • 1
  • 12

1 Answers1

5

This code will create a new paragraph style with 1/2 inch tab intervals and set it as both the default and current paragraph style on the variable textView.

NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
[style setDefaultTabInterval:36.];
[style setTabStops:[NSArray array]];
[textView setDefaultParagraphStyle:style];
[textView setTypingAttributes:[NSDictionary dictionaryWithObject:style forKey:NSParagraphStyleAttributeName]];
[style release];
ughoavgfhw
  • 39,734
  • 6
  • 101
  • 123
  • I added the proposed code in a subclass of nstextview ( of course changing textView by self ). Still i am getting just 12 tabstops, at 4 characters per stop ( im using a monospaced font, monaco ). – Manuel Astudillo Feb 16 '11 at 22:10
  • Could you post it with context? I copied this code from one of my projects, and it has worked fine for me. – ughoavgfhw Feb 16 '11 at 23:35
  • I made it work. The problem was that I was creating the content in the text view programmatically adding NSAttributedString objects. I just put your proposed style in the attributed string and everything worked. thanks. – Manuel Astudillo Feb 18 '11 at 20:36
  • You should mark as the "Accepted answer" if it answers your question. – Donovan Oct 14 '11 at 13:17
  • I tried this. It didn't work for me using Xcode 5 on OS X 10.8.5. There's no text being programmatically added. – ArtOfWarfare Oct 17 '13 at 21:41
  • Nevermind. It worked perfectly. Upon inspection, I found `textView` was `null` because I put this code into my `-init` method instead of my `-awakeFromNib`. Fixing that fixed my problem. – ArtOfWarfare Oct 17 '13 at 22:17
  • Thank you, this was a simple solution for a problem that took much more searching to solve than anticipated! – nuclearnova Dec 03 '14 at 21:42