我試圖計算所需的NSTextView
的內容在一組寬度通過以下過程(其中self
是NSTextView
一個實例)的最小高度:usedRectForTextContainer大小錯誤與NSAttributedString?
let currentWidth = self.frame.width
let textStorage = NSTextStorage(attributedString: self.attributedString())
let textContainer = NSTextContainer(containerSize: NSMakeSize(currentWidth, CGFloat.max))
let layoutManager = NSLayoutManager()
layoutManager.addTextContainer(textContainer)
textStorage.addLayoutManager(layoutManager)
layoutManager.glyphRangeForTextContainer(textContainer)
let newSize = layoutManager.usedRectForTextContainer(textContainer)
heightConstraint.constant = newSize.height
字符串本身通過轉換來創建從降價到NSAttributedString
:
let data = styledHTML.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)
let attributed = try! NSAttributedString(data: data!, options: [
NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType
], documentAttributes: nil)
self.textStorage?.setAttributedString(attributed)
問題,但是,是計算的最小高度是關閉的20像素(灰色區域指示文本中,紅色表示視圖):
我想這是與usedRectForTextContainer()
的問題,但是當我設置的string
值NSTextView
實例別的東西,即:
self.string = "Random string...\nTwo lines"
let currentWidth = self.frame.width
...
我得到正確的高度:
我還沒有發現任何對谷歌有用的東西,除了this question有一個類似的問題,但沒有解決方案。
這可能是值得指出的是,我設置通過串聯樣式表中的灰色背景下,已初具雛形:
*{
margin:0 !important;
padding:0 !important;
line-height:20px;
/*DEFAULT_FONT*/
/*DEFAULT_FONT_SIZE*/
background:grey;
}
em{
font-style:italic;
}
凡/*DEFAULT_FONT*/
和/*DEFAULT_FONT_SIZE*/
被添加到HTML之前換成默認NSFont
值。
編輯:這不是生成的HTML引起的差異,因爲它們都具有完全一樣的格式/風格:
原始字符串:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta http-equiv="Content-Style-Type" content="text/css">
<title></title>
<meta name="Generator" content="Cocoa HTML Writer">
<meta name="CocoaVersion" content="1404.11">
<style type="text/css">
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; line-height: 20.0px; font: 13.0px 'Helvetica Neue'; color: #0000ee; -webkit-text-stroke: #0000ee; background-color: #808080}
span.s1 {text-decoration: underline ; font-kerning: none}
</style>
</head>
<body>
<p class="p1"><span class="s1"><a href="http://www.native-languages.org/houses.htm">http://www.native-languages.org/houses.htm</a></span></p>
<p class="p1"><span class="s1"><a href="https://www.youtube.com/watch?v=1oU6__m8To4">https://www.youtube.com/watch?v=1oU6__m8To4</a></span></p>
</body>
</html>
隨機字符串:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta http-equiv="Content-Style-Type" content="text/css">
<title></title>
<meta name="Generator" content="Cocoa HTML Writer">
<meta name="CocoaVersion" content="1404.11">
<style type="text/css">
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; line-height: 20.0px; font: 13.0px 'Helvetica Neue'; color: #0000ee; -webkit-text-stroke: #0000ee; background-color: #808080}
span.s1 {text-decoration: underline ; font-kerning: none}
</style>
</head>
<body>
<p class="p1"><span class="s1"><a href="http://www.native-languages.org/houses.htm">F</a></span></p>
</body>
</html>