如何将NSTextView限制为2行?

问题描述 投票:4回答:3

我正在尝试为NSTextView指定行数。我的设计师要求最多两行文字。我已经尝试过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)

我在NSTextView上尝试过高度限制。我试过了:

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

我已经尝试为NSScrollView创建IBOutlet NSTextView并调整其高度。获得两条线和截断都没有运气。任何帮助是极大的赞赏。我觉得我只是错过了方法或设置。谢谢!

macos cocoa nstextview nsmutableattributedstring nsparagraphstyle
3个回答
1
投票

您可以使用配置为多行标签的NSTextField。这意味着将其cellwraps属性设置为true,如果需要,将其truncatesLastVisibleLine设置为true。


8
投票

从10.11开始,您可以使用它

yourTextViewObj.textContainer.maximumNumberOfLines = 2;

0
投票

对于NSTextField(又名标签)你可以做self.textField.maximumNumberOfLines = 2;

而已。

© www.soinside.com 2019 - 2024. All rights reserved.