Thursday, July 9, 2020

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

stormyhamster
Member
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:

Applescript:


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?

TIA.

—Storm Hampusson


Filed under: NSTextView, lineheight

Offline

 

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

Shane Stanley
Member
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:

Applescript:

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 <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

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

stormyhamster
Member
Registered: 2019-02-03
Posts: 2

Re: Setting Line Height in NSTextView

Thanks for the help, Shane – much appreciated.

—Storm

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)