2015-10-15 51 views
1

我試圖計算所需的NSTextView的內容在一組寬度通過以下過程(其中selfNSTextView一個實例)的最小高度: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像素(灰色區域指示文本中,紅色表示視圖):

enter image description here

我想這是與usedRectForTextContainer()的問題,但是當我設置的stringNSTextView實例別的東西,即:

self.string = "Random string...\nTwo lines" 
let currentWidth = self.frame.width 
... 

我得到正確的高度:

enter image description here

我還沒有發現任何對谷歌有用的東西,除了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> 

回答

0

那麼這有點尷尬!在進一步測試之後,我注意到self.attributedString().string的值有一個尾部換行符\n,它必須在從HTML到NSAttributedString的轉換過程中創建。我糾正它,像這樣:

let data = styledHTML.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false) 
let attributed = try! NSMutableAttributedString(data: data!, options: [ 
    NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType 
], documentAttributes: nil) 
let lastCharacterRange = NSMakeRange(commentString.length - 1, 1)  
let lastCharacter = self.attributedSubstringFromRange(lastCharacterRange) 
if lastCharacter.string == "\n" { 
    self.deleteCharactersInRange(lastCharacterRange) 
} 

基本上,我改變了NSAttributedStringNSMutableAttributedString,讓我通過deleteCharactersInRange刪除最後一個字符。