简体   繁体   中英

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!

从 10.11 你可以使用这个

yourTextViewObj.textContainer.maximumNumberOfLines = 2;

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.

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

That's it.

最大行数现在是 NSTextField 的属性

label.maximumNumberOfLines = 1;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM