简体   繁体   English

如何将 NSTextView 限制为 2 行?

[英]How do I limit NSTextView to 2 lines?

I'm trying to specify the number of lines for NSTextView.我正在尝试为 NSTextView 指定行数。 My designer is requesting 2 lines of text max.我的设计师要求最多 2 行文本。 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.我已经尝试过 NSMutableParagraph 样式来添加我想要的省略号截断,但是使用 NSMutableParagraph 我只能获得 1 行的 NSTextView,而没有 NSMutableParagraph,我得到了一个滚动文本,其中包含完成文本所需的行数。

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.我在 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.我已经尝试为 NSScrollView 创建 IBOutlet,在其中的 NSTextView 并调整其高度。 No luck with getting both 2 lines and truncation.没有获得 2 行和截断的运气。 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.您可以使用配置为多行标签的NSTextField That means setting its cell 's wraps property to true and, if desired, its truncatesLastVisibleLine to true.这意味着将其cellwraps属性设置为 true,如果需要,将其truncatesLastVisibleLine为 true。

For NSTextField (aka label) You can just do self.textField.maximumNumberOfLines = 2;对于 NSTextField(又名标签)你可以只做 self.textField.maximumNumberOfLines = 2;

That's it.就是这样。

最大行数现在是 NSTextField 的属性

label.maximumNumberOfLines = 1;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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