Swift 3 NSTextView with long line breaks when including tabs

380 views Asked by At

I have a NSTextView to display long lines of numeric data, separated by tabs. I cannot prevent the line break after a fixed length. As a test. I use to Strings: Displaying a long line of pure textA works fine:
enter image description here
TextB breaks the line after 9 chunks: enter image description here My code: (downloadDataFeld is the NSTextView)

let textA = "asdfgh  asdfgh  asdfgh  asdfgh  asdfgh  asdfgh  asdfgh  asdfgh  asdfgh  asdfgh  asdfgh  asdfgh  asdfgh  asdfgh  asdfgh  asdfgh  asdfgh  asdfgh  asdfgh  asdfgh    \n"
let textB = "asdfgh  \tasdfgh  \tasdfgh  \tasdfgh  \tasdfgh  \tasdfgh  \tasdfgh  \tasdfgh  \tasdfgh  \tasdfgh  \tasdfgh  \tasdfgh  \tasdfgh  \tasdfgh  \tasdfgh  \tasdfgh  \n"

let paragraphStyle = NSMutableParagraphStyle()
let tabInterval:CGFloat = 20.0
let tabs:NSMutableArray = NSMutableArray.init()

for cnt in 0..<24 
{    // Add 24 tab stops, at desired intervals...
   paragraphStyle.tabStops.append(NSTextTab(textAlignment: .left, location: (CGFloat(cnt) * tabInterval), options: [:]))
}

let attrtext = NSMutableAttributedString(string: textB)
let textRange = NSMakeRange(0, textB.characters.count)
attrtext.addAttribute(NSParagraphStyleAttributeName, value: paragraphStyle, range: textRange)
// load the text
downloadDataFeld.textStorage?.append(attrtext)

The TextView settings are:
(I had to set the with of the documentView explicitly in code to get the working result without tabs)

downloadDataFeld.delegate = self
  downloadDataFeld.enclosingScrollView?.documentView?.frame.size.width = 2000 // muss
  downloadDataFeld.enclosingScrollView?.hasHorizontalScroller = true
  downloadDataFeld.enclosingScrollView?.hasVerticalScroller = true
  downloadDataFeld.enclosingScrollView?.autohidesScrollers = false

I couldn't find anything to set the line break. What am I missing?

0

There are 0 answers