Truncate NSTextView?

亡梦爱人 提交于 2019-11-30 14:43:31

If you hover over the corresponding control in IB, you can see the declaration and brief documentation on the method that it triggers (usually the getter rather than the setter, but it's easy enough to infer one from the other). So for example for the "Line Breaks" popup, you'll see the lineBreakMode method. Then you can use your favorite method to find it in Xcode (e.g. "Open Quickly").

So what you want is:

[[someTextField cell] setLineBreakMode:NSLineBreakByTruncatingTail];

or potentially:

[[someTextField cell] setLineBreakMode:NSLineBreakByCharWrapping];
[[someTextField cell] setTruncatesLastVisibleLine:YES];

It is correct what Nicholas wrote.

I like to add (because I spend some time searching for it) if you use Auto Layout for your NSTextView and surrounding NSView you should set your Content Compression Resistance Priority of the NSTextView less than NSLayoutPriorityWindowSizeStayPut (e.g. 499). Otherwise your NSTextView will not truncate its content.

Another title for this could have been:

How to STOP an NSTextField from resizing your window when its contents are longer than your window!

Combining both Nicholas & Stephan's answers is what helped me.

I placed…

[[someTextField cell] setLineBreakMode:NSLineBreakByTruncatingTail];

…in…

- (void)windowDidLoad

Or, for those that like to use interface builder…

Use Stephan's solution and change the NSTextField's Layout to Truncates. Also, check to see what the Line Break is - Truncate Tail worked for what I needed.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!