Setting Line Height in NSTextView

Hi folks,

I’m trying to set the line height of text in an NSTextView in AppleScriptObjC; I know this is probably ridiculously simple, but I have literally been struggling for hours (don’t laugh) and have finally thrown the proverbial towel in.

This is the code that I’m trying to add a line height setting to:

tell (NSTextView's alloc's initWithFrame:{{10, -10}, {400, 200}})
	set theTextView to it
	its setAlignment:NSTextAlignmentLeft
	its setFont:(NSFont's fontWithName:"SF Pro Display Regular" |size|:48)
	its setTextColor:(NSColor's orangeColor)
end tell

This successfully creates a text view with left-aligned 48pt orange text, but I’d now like to be able to adjust the space between the lines of text. I know that the solution seemingly involves NSMutableParagraphStyle (and perhaps NSMutableDictionary) combined with setLineHeightMultiple (for example), but my ageing brain just cannot put the pieces together. Can anyone help?


—Storm Hampusson

The more typical approach is to style text itself, via the textStorage property. So something like this:

set parStyle to current application's NSParagraphStyle's defaultParagraphStyle()'s mutableCopy()
parStyle's setLineHeightMultiple:1.3
parStyle's setAlignment:(current application's NSTextAlignmentLeft)
set theFont to current application's NSFont's fontWithName:"SF Pro Display Regular" |size|:48.0
set theColor to current application's NSColor's orangeColor()
set styleDict to current application's NSMutableDictionary's dictionaryWithObjects:{theFont, theColor, parStyle} forKeys:{current application's NSFontAttributeName, current application's NSForegroundColorAttributeName, current application's NSParagraphStyleAttributeName}

set theTextView to current application's NSTextView's alloc's initWithFrame:{{10, -10}, {400, 200}}
set theText to theTextView's textStorage()
theText's addAttributes:styleDict range:{0, theText's |length|()}

Thanks for the help, Shane – much appreciated.