9

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!

myData
  • 215
  • 1
  • 4
  • 16
  • Have you considered using a text **field**? You can set the text field cell's `truncatesLastVisibleLine` property. Also, do you need to allow editing but prevent the user from entering more text than will fit on two lines? For a text *view*, have you set `verticallyResizable` to false? Have you set `maxSize`? – Ken Thomases Sep 19 '15 at 09:22
  • @KenThomases, yes, I've considered a textfield. I actually have a NSTextField for another property on this view. TextField only displays one line. For the textView, I have tried setting verticallyResizable to false and setting maxSize. – myData Sep 19 '15 at 15:22
  • 1
    You can make multi-line text fields. – Ken Thomases Sep 19 '15 at 15:52
  • Yes, I ran across that and it was easy in iOS, but I'm not finding the right procedure for OSX. – myData Sep 19 '15 at 16:05
  • Also, these fields are not to be editable by user, they are only displaying data pulled from API. – myData Sep 19 '15 at 16:07
  • Just drag a multi-line label from the Object library into the view. – Ken Thomases Sep 19 '15 at 16:25
  • I was able to solve programmatically with: let cell = cellView.myLabel!.cell() as! NSTextFieldCell cell.wraps = true cell.scrollable = false – myData Sep 19 '15 at 16:36
  • If you post your suggestion by answering the question instead of with a comment, I will mark it as correct. Thank you for your help @KenThomases – myData Sep 19 '15 at 16:37

4 Answers4

11

From 10.11 you can use this

yourTextViewObj.textContainer.maximumNumberOfLines = 2;
palaniraja
  • 10,432
  • 5
  • 43
  • 76
9

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.

Ken Thomases
  • 88,520
  • 7
  • 116
  • 154
1

For NSTextField (aka label) You can just do self.textField.maximumNumberOfLines = 2;

That's it.

coolcool1994
  • 3,704
  • 4
  • 39
  • 43
0

Max number of lines is now a property of NSTextField

label.maximumNumberOfLines = 1;
Cliff Ribaudo
  • 8,932
  • 2
  • 55
  • 78