I am attempting to calculate the minimum height needed for an NSTextView
's content at a set width through the following process (where self
is an instance of 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
The string itself is created through a conversion from markdown to an NSAttributedString
:
let data = styledHTML.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)
let attributed = try! NSAttributedString(data: data!, options: [
NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType
], documentAttributes: nil)
self.textStorage?.setAttributedString(attributed)
The issue, however, is that the minimum height calculated is off by 20px (the grey area indicates the text, the red indicates the view):
I figured this was an issue with usedRectForTextContainer()
, however when I set the string
value of the NSTextView
instance to something else, i.e.:
self.string = "Random string...\nTwo lines"
let currentWidth = self.frame.width
...
I get the correct height:
I haven't found anything too useful on Google, other than this question which has a similar issue but no solution.
It might be worth noting that I am setting the grey background through an inline style sheet that is pretty barebones:
*{
margin:0 !important;
padding:0 !important;
line-height:20px;
/*DEFAULT_FONT*/
/*DEFAULT_FONT_SIZE*/
background:grey;
}
em{
font-style:italic;
}
Where /*DEFAULT_FONT*/
and /*DEFAULT_FONT_SIZE*/
are swapped for default NSFont
values prior to being added to the HTML.
Edit: It's not the HTML generated that causes the discrepancy, as they both have the exact same format/styling:
Original string:
<!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>
Random string:
<!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>
Well this is slightly embarrassing! After testing this further, I noticed that the self.attributedString().string
value had a trailing line break \n
, which must've been created during the conversion from HTML to an NSAttributedString
. I remedied it like so:
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)
}
Basically, I changed the NSAttributedString
to an NSMutableAttributedString
, allowing me to remove the last character through deleteCharactersInRange
.