簡體   English   中英

如何將 NSTextView 限制為 2 行?

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

我正在嘗試為 NSTextView 指定行數。 我的設計師要求最多 2 行文本。 我已經嘗試過 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 並調整其高度。 沒有獲得 2 行和截斷的運氣。 任何幫助是極大的贊賞。 我覺得我只是缺少一種方法或設置。 謝謝!

從 10.11 你可以使用這個

yourTextViewObj.textContainer.maximumNumberOfLines = 2;

您可以使用配置為多行標簽的NSTextField 這意味着將其cellwraps屬性設置為 true,如果需要,將其truncatesLastVisibleLine為 true。

對於 NSTextField(又名標簽)你可以只做 self.textField.maximumNumberOfLines = 2;

就是這樣。

最大行數現在是 NSTextField 的屬性

label.maximumNumberOfLines = 1;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM