【问题标题】:Can't disable word wrap of NSTextView无法禁用 NSTextView 的自动换行
【发布时间】:2016-09-03 15:24:06
【问题描述】:

我已经尝试了How to disable word-wrap of NSTextView? 中的答案 半天,但没有运气。答案真的有点分散和混乱。

我有这个代码:

@IBOutlet var display: NSTextView!

func applicationDidFinishLaunching(aNotification: NSNotification) {
    // Insert code here to initialize your application
    let LargeNumberForText: CGFloat = 1.0e7
    display.textContainer!.containerSize = NSMakeSize(LargeNumberForText, LargeNumberForText)
    display.textContainer!.widthTracksTextView = false
    display.horizontallyResizable = true
    display.autoresizingMask = [.ViewWidthSizable, .ViewHeightSizable]
}

我在 .xib 中有这个:

我错过了一步吗?

【问题讨论】:

    标签: swift cocoa word-wrap nstextview


    【解决方案1】:

    我的问题实际上是 Premature line wrapping in NSTextView when tabs are used 我通过使用上面的自动换行代码纠正了这个问题,并在更改文本后调用它:

    func format() {
        let numStops = 100000;
        let tabInterval = 40;
        var tabStop: NSTextTab
    
        //attributes for attributed String of TextView
    
        let paraStyle = NSMutableParagraphStyle()
    
        // This first clears all tab stops, then adds tab stops, at desired intervals...
        paraStyle.tabStops = []
        for cnt in 1...numStops {
            tabStop = NSTextTab(type: .LeftTabStopType, location: CGFloat(tabInterval * cnt))
            paraStyle.addTabStop(tabStop)
        }
    
        var attrs = [String: AnyObject]()
        attrs.updateValue(paraStyle, forKey:NSParagraphStyleAttributeName)
    
        display.textStorage!.addAttributes(attrs, range: NSMakeRange(0, display.textStorage!.string.characters.count))
    }
    

    display 是 NSTextView。当然,子类化会更优雅。

    【讨论】:

      猜你喜欢
      • 2015-09-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-16
      相关资源
      最近更新 更多