How do I limit NSTextView to 2 lines?
Asked Answered
L

4

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!

Landri answered 19/9, 2015 at 8:5 Comment(8)
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?Vamp
@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.Landri
You can make multi-line text fields.Vamp
Yes, I ran across that and it was easy in iOS, but I'm not finding the right procedure for OSX.Landri
Also, these fields are not to be editable by user, they are only displaying data pulled from API.Landri
Just drag a multi-line label from the Object library into the view.Vamp
I was able to solve programmatically with: let cell = cellView.myLabel!.cell() as! NSTextFieldCell cell.wraps = true cell.scrollable = falseLandri
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 @KenThomasesLandri
V
10

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.

Vamp answered 19/9, 2015 at 18:38 Comment(1)
Also, have to set maximumNumberOfLines.Catchings
T
11

From 10.11 you can use this

yourTextViewObj.textContainer.maximumNumberOfLines = 2;
Tremulous answered 27/2, 2017 at 15:37 Comment(0)
V
10

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.

Vamp answered 19/9, 2015 at 18:38 Comment(1)
Also, have to set maximumNumberOfLines.Catchings
G
1

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

That's it.

Geri answered 2/1, 2018 at 3:0 Comment(1)
Also, need to cell's wraps property.Catchings
G
0

Max number of lines is now a property of NSTextField

label.maximumNumberOfLines = 1;
Guanabara answered 15/5, 2020 at 11:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.