使用NSAttributedString的usedRectForTextContainer大小错误?

时间:2015-10-15 05:07:38

标签: swift cocoa nsattributedstring

我正在尝试通过以下过程计算NSTextView内容在设定宽度下所需的最小高度(其中selfNSTextView的实例):

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)

但问题是,计算出的最小高度偏差为20px(灰色区域表示文本,红色表示视图):

enter image description here

我认为这是usedRectForTextContainer()的问题,但是当我将string实例的NSTextView值设置为其他内容时,即:

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

我得到正确的身高:

enter image description here

我没有在Google上找到任何有用的内容,除this question之外有类似问题但没有解决方案。

值得注意的是,我通过内联样式表设置了灰色背景:

*{
    margin:0 !important;
    padding:0 !important;
    line-height:20px;
    /*DEFAULT_FONT*/
    /*DEFAULT_FONT_SIZE*/
    background:grey;
}
em{
    font-style:italic;
}

在添加到HTML之前,将/*DEFAULT_FONT*//*DEFAULT_FONT_SIZE*/替换为默认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>

1 个答案:

答案 0 :(得分: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)
}

基本上,我将NSAttributedString更改为NSMutableAttributedString,允许我通过deleteCharactersInRange删除最后一个字符。