Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I limit NSTextView to 2 lines?

I'm trying to specify the number of lines for NSTextView. My designer is requesting 2 lines of text max. I've tried NSMutableParagraph style to add the ellipses truncation that I want, but with NSMutableParagraph I can only get NSTextView with 1 line and without NSMutableParagraph, I get a scrolling text with as many lines as needed to complete text.

var attributedString = NSMutableAttributedString(string: "This is my text, I can keep going for many characters")
var para = NSMutableParagraphStyle()
para.lineBreakMode = NSLineBreakMode.ByTruncatingTail
let globalAttributes = [
  NSParagraphStyleAttributeName: para
]
let range = NSRange(location:0, length: attributedString.length)
attributedString.addAttributes(globalAttributes, range: range)
cellView.myTextView!.textStorage?.setAttributedString(attributedString)

I've tried height constraint on NSTextView. I've tried:

cellView.myTextView!.textContainer?.containerSize = NSMakeSize(300, 32)

I've tried creating IBOutlet for NSScrollView that NSTextView in within and adjusting its height. No luck with getting both 2 lines and truncation. Any help is greatly appreciated. I feel like I'm just missing a method or setup. Thanks!

like image 490
myData Avatar asked Sep 19 '15 08:09

myData


2 Answers

From 10.11 you can use this

yourTextViewObj.textContainer.maximumNumberOfLines = 2;
like image 101
palaniraja Avatar answered Nov 20 '22 04:11

palaniraja


You can use an NSTextField configured as a multi-line label. That means setting its cell's wraps property to true and, if desired, its truncatesLastVisibleLine to true.

like image 8
Ken Thomases Avatar answered Nov 20 '22 02:11

Ken Thomases