我试图通过以下过程计算 NSTextView
内容在设定宽度下所需的最小高度(其中 self
是 的实例NSTextView
):
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
字符串本身是通过从 markdown 到 NSAttributedString
的转换创建的:
let data = styledHTML.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)
let attributed = try! NSAttributedString(data: data!, options: [
NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType
], documentAttributes: nil)
self.textStorage?.setAttributedString(attributed)
但是,问题是计算出的最小高度有 20 像素的偏差(灰色区域表示文本,红色区域表示 View ):
我认为这是 usedRectForTextContainer()
的问题,但是当我将 NSTextView
实例的 string
值设置为其他值时,即:
self.string = "Random string...\nTwo lines"
let currentWidth = self.frame.width
...
我得到正确的高度:
除了 this question,我在 Google 上没有发现任何有用的东西它有类似的问题,但没有解决方案。
可能值得注意的是,我通过一个非常简单的内联样式表来设置灰色背景:
*{
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>
最佳答案
这有点尴尬!进一步测试后,我注意到 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
删除最后一个字符。
关于swift - usedRectForTextContainer 大小错误与 NSAttributedString?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33140284/