如何禁用NSTextView的自动换行? [英] How to disable word-wrap of NSTextView?

查看:1506
本文介绍了如何禁用NSTextView的自动换行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

NSTextView默认情况下会自动换行。如何禁用这个?
我在做一个JSON代码查看器,所以我必须禁用这个。

解决方案





更新



我发现我的旧示例代码不足以使其完全正常工作。 (因为SDK版本?)
也是
这是我的完整源代码片段,禁用在OSX 10.8 SDK中的自动换行。

  [self setMaxSize:CGSizeMake(FLT_MAX,FLT_MAX)]; 
[self setHorizo​​ntallyResizable:YES];
[[self textContainer] setWidthTracksTextView:NO];
[[self textContainer] setContainerSize:CGSizeMake(FLT_MAX,FLT_MAX)];



更新2



=https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/TextUILayer/Tasks/TextInScrollView.html =nofollow noreferrer> Apple正在提供官方指南来创建 NSTextView 。我希望这有助于。



更新3



我发布了在Github上的示例项目。有关具体实施,请参阅此页面: https:/ /github.com/Eonil/CocoaProgrammaticHowtoCollection/blob/master/ComponentUsages/TextView/ExampleApplicationController.swift?ts=4



以下是示例中的代码段项目。

  if wordwrap {
///匹配宽度在这里也很重要。
let sz1 = scroll1.contentSize
text1.frame = CGRect(x:0,y:0,width:sz1.width,height:0)
text1.textContainer!.containerSize = CGSize (width:sz1.width,height:CGFloat.max)
text1.textContainer!widthTracksTextView = true
} else {
text1.textContainer!widthTracksTextView = false
text1。 textContainer!.containerSize = CGSize(width:CGFloat.max,height:CGFloat.max)
}


NSTextView does word-wrap by default. How can I disable this? I'm making a JSON code viewer, so I have to disable this.

解决方案

I got solution from: http://lists.apple.com/archives/cocoa-dev/2008/May/msg02396.html

You have to set NSTextView's maximum width to very large number to make this work correctly. (Just copy maximum height) And enable horizontal scrolling of NSScrollView which is superview of the NSTextView. See these pictures:

http://www.flickr.com/photos/47601728@N06/4759470529/

http://www.flickr.com/photos/47601728@N06/4759470533/

Update

I discovered my old sample code was insufficient to make it fully work correctly. (because of SDK version?) Also Here's my full source code snippet which disables word-wrap in OSX 10.8 SDK.

[self setMaxSize:CGSizeMake(FLT_MAX, FLT_MAX)];    
[self setHorizontallyResizable:YES];               
[[self textContainer] setWidthTracksTextView:NO];  
[[self textContainer] setContainerSize:CGSizeMake(FLT_MAX, FLT_MAX)];  

Update 2

Now Apple is providing an official guide to create NSTextView correctly. I hope this helps.

Update 3

I posted an example project on Github. See this page for specific implementation: https://github.com/Eonil/CocoaProgrammaticHowtoCollection/blob/master/ComponentUsages/TextView/ExampleApplicationController.swift?ts=4

Here's a code snippet from the sample project.

        if wordwrap {
            /// Matching width is also important here.
            let sz1                                     =   scroll1.contentSize
            text1.frame                                 =   CGRect(x: 0, y: 0, width: sz1.width, height: 0)
            text1.textContainer!.containerSize          =   CGSize(width: sz1.width, height: CGFloat.max)
            text1.textContainer!.widthTracksTextView    =   true
        } else {
            text1.textContainer!.widthTracksTextView    =   false
            text1.textContainer!.containerSize          =   CGSize(width: CGFloat.max, height: CGFloat.max)
        }

这篇关于如何禁用NSTextView的自动换行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆