Thursday, July 9, 2020

#1 2020-06-16 09:03:57 pm

Registered: 2019-02-03
Posts: 2

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

Filed under: NSTextView, lineheight



#2 2020-06-16 10:10:17 pm

Shane Stanley
From:: Australia
Registered: 2002-12-07
Posts: 6397

Re: Setting Line Height in NSTextView

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|()}

Shane Stanley <>



#3 2020-06-18 07:02:56 am

Registered: 2019-02-03
Posts: 2

Re: Setting Line Height in NSTextView

Thanks for the help, Shane – much appreciated.




Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)