Setting the Line Height/ Line Spacing in an NSTextView.

272 views
Skip to first unread message

Joshua Garnham

unread,
Dec 24, 2009, 1:25:01 PM12/24/09
to coco...@lists.apple.com
How would I set the Line Height/ Line Spacing in an NSTextView? e.g How
tall each line is or how much space is between each line.

Thanks.


_______________________________________________

Cocoa-dev mailing list (Coco...@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/cocoa-dev-garchive-98506%40googlegroups.com

This email sent to cocoa-dev-ga...@googlegroups.com

Douglas Davidson

unread,
Dec 24, 2009, 2:40:09 PM12/24/09
to Joshua Garnham, coco...@lists.apple.com
Use a paragraph style.

Douglas Davidson

On Dec 24, 2009, at 10:25 AM, Joshua Garnham
<joshua....@yahoo.co.uk> wrote:

> How would I set the Line Height/ Line Spacing in an NSTextView? e.g
> How
> tall each line is or how much space is between each line.
>
> Thanks.
>
>
>
>
> _______________________________________________
>
> Cocoa-dev mailing list (Coco...@lists.apple.com)
>
> Please do not post admin requests or moderator comments to the list.
> Contact the moderators at cocoa-dev-admins(at)lists.apple.com
>
> Help/Unsubscribe/Update your Subscription:

> http://lists.apple.com/mailman/options/cocoa-dev/ddavidso%40apple.com
>
> This email sent to ddav...@apple.com

Graham Cox

unread,
Dec 25, 2009, 6:25:16 PM12/25/09
to Joshua Garnham, coco...@lists.apple.com

On 25/12/2009, at 5:25 AM, Joshua Garnham wrote:

> How would I set the Line Height/ Line Spacing in an NSTextView? e.g How
> tall each line is or how much space is between each line.


These are all properties of a NSParagraphStyle, which is itself a property applied to a range of characters within an NSAttributedString (which in turn is owned by a NSTextView as its -textStorage). That should be enough for you to locate the relevant docs and proceed.

--Graham

Joshua Garnham

unread,
Dec 30, 2009, 10:43:09 AM12/30/09
to coco...@lists.apple.com
How would I set the Line Height/ Line Spacinh in an NSTextView? e.g How

tall each line is or how much space is between each line.

Here's what I've tried (but doesn't work), it is in a NSTextView subclass …
- (void)setDefaultParagraphStyle:(NSMutableParagraphStyle *)paragraphStyle {
CGFloat spacing = 5.0f;
[paragraphStyle setLineSpacing:spacing];
[paragraphStyle setMinimumLineHeight:spacing];
[paragraphStyle setMaximumLineHeight:spacing];
}

What's wrong with it?

Ross Carter

unread,
Dec 30, 2009, 11:12:18 AM12/30/09
to Joshua Garnham, coco...@lists.apple.com
On Dec 30, 2009, at 10:43 AM, Joshua Garnham wrote:

> How would I set the Line Height/ Line Spacinh in an NSTextView? e.g How
> tall each line is or how much space is between each line.
>
> Here's what I've tried (but doesn't work), it is in a NSTextView subclass …
> - (void)setDefaultParagraphStyle:(NSMutableParagraphStyle *)paragraphStyle {
> CGFloat spacing = 5.0f;
> [paragraphStyle setLineSpacing:spacing];
> [paragraphStyle setMinimumLineHeight:spacing];
> [paragraphStyle setMaximumLineHeight:spacing];
> }
>
> What's wrong with it?

A couple of things. First, the NSTextView method is - (void)setDefaultParagraphStyle:(NSParagraphStyle *)paragraphStyle. The argument is a regular NSParagraphStyle, not the mutable variety.

Second, rather than override - (void)setDefaultParagraphStyle:(NSParagraphStyle *)paragraphStyle, you simply use it as is. Construct your paragraphStyle elsewhere, and then send it to -setDefaultParagraphStyle:._______________________________________________

Fritz Anderson

unread,
Dec 30, 2009, 11:14:33 AM12/30/09
to Joshua Garnham, coco...@lists.apple.com
On 30 Dec 2009, at 9:43 AM, Joshua Garnham wrote:

> How would I set the Line Height/ Line Spacinh in an NSTextView? e.g How
> tall each line is or how much space is between each line.
>
> Here's what I've tried (but doesn't work), it is in a NSTextView subclass …
> - (void)setDefaultParagraphStyle:(NSMutableParagraphStyle *)paragraphStyle {
> CGFloat spacing = 5.0f;
> [paragraphStyle setLineSpacing:spacing];
> [paragraphStyle setMinimumLineHeight:spacing];
> [paragraphStyle setMaximumLineHeight:spacing];
> }
>
> What's wrong with it?

That you modify a proposed default paragraph style, but don't actually SET the paragraph style? Such as by [super setDefaultParagraphStyle:]?

Also, declaring that the parameter is a _mutable_ paragraph style does not make the parameter you are given actually mutable. The declaration of the method specifies NSParagraphStyle, and you must assume you can't change it.

Do you ensure that this method actually gets called? Have you a theory on why you're overriding a possible setting of the default style, instead of just doing the setting yourself?

— F

Joshua Garnham

unread,
Dec 30, 2009, 12:01:25 PM12/30/09
to Ross Carter, coco...@lists.apple.com
But the methodssetLineSpacing: and others are NSMutableParagraphStyle methods only.


________________________________
From: Ross Carter <rossca...@me.com>
To: Joshua Garnham <joshua....@yahoo.co.uk>
Cc: coco...@lists.apple.com
Sent: Wed, 30 December, 2009 16:12:18
Subject: Re: Setting the Line Height/ Line Spacing in an NSTextView.

Fritz Anderson

unread,
Dec 30, 2009, 12:24:21 PM12/30/09
to Joshua Garnham, cocoa-dev List
On 30 Dec 2009, at 11:01 AM, Joshua Garnham wrote:

> But the methodssetLineSpacing: and others are NSMutableParagraphStyle methods only.

That's the point. You may _want_ to change the paragraph style's properties, but you can't. Casting the parameter doesn't turn an immutable object into a mutable one.

If you really want to substitute a paragraph style of your own (and please comment on the suggestions that you may not want to), make a mutableCopy, set that up as you wish, and pass it up to super.

You may be misconceiving the need to subclass in Cocoa. Subclassing is rare. Most modifications to object behaviors are done through delegates and property setters; Cocoa provides a lot of them. That's why you're getting told to _call_ setDefaultParagraphStyle:, not _override_ it.

If you explained why you feel you must override, we might be better able to help you.

— F

Joshua Garnham

unread,
Dec 31, 2009, 1:14:42 PM12/31/09
to Fritz Anderson, coco...@lists.apple.com
Thanks, there is no immediate need to override so I will try calling it.

So would this work?…

CGFloat spacing = 5.0f;
NSMutablePargagraphStyle *paragraphStyle;


[paragraphStyle setLineSpacing:spacing];
[paragraphStyle setMinimumLineHeight:spacing];
[paragraphStyle setMaximumLineHeight:spacing];

[textView setDefaultParagraphStyle:paragraphStyle];

- Josh


________________________________
From: Fritz Anderson <fri...@manoverboard.org>
To: Joshua Garnham <joshua....@yahoo.co.uk>
Cc: cocoa-dev List <coco...@lists.apple.com>
Sent: Wed, 30 December, 2009 17:24:21


Subject: Re: Setting the Line Height/ Line Spacing in an NSTextView.

On 30 Dec 2009, at 11:01 AM, Joshua Garnham wrote:

Andy Lee

unread,
Dec 31, 2009, 1:18:54 PM12/31/09
to Joshua Garnham, coco...@lists.apple.com
On Dec 31, 2009, at 1:14 PM, Joshua Garnham wrote:
> So would this work?…
>
> CGFloat spacing = 5.0f;
> NSMutablePargagraphStyle *paragraphStyle;
> [paragraphStyle setLineSpacing:spacing];
> [paragraphStyle setMinimumLineHeight:spacing];
> [paragraphStyle setMaximumLineHeight:spacing];
> [textView setDefaultParagraphStyle:paragraphStyle];

Why don't you tell us?

--Andy

Graham Cox

unread,
Dec 31, 2009, 7:16:04 PM12/31/09
to Joshua Garnham, Cocoa-Dev List

On 01/01/2010, at 5:14 AM, Joshua Garnham wrote:

> So would this work?…

> NSMutablePargagraphStyle *paragraphStyle;
> [paragraphStyle setLineSpacing:spacing];

No - you haven't allocated/inited the paragraphStyle object, nor initialised the variable. Most probably this will crash. Also, there's no such class as NSMutablePargagraphStyle.

I'm guessing this is 'pseudocode', but it's really better not to do that but to write exactly what you mean. It saves a lot of potential misinterpretations.

--Graham

Joshua Garnham

unread,
Jan 1, 2010, 3:10:04 AM1/1/10
to Graham Cox, coco...@lists.apple.com
That was a spelling mistake, it was meant to say NSMutableParagraphStyle.
I've tried doing this …

CGFloat spacing = 5.0f;
NSMutableParagraphStyle *paragraphStyle;
[paragraphStyle init];


[paragraphStyle setLineSpacing:spacing];
[paragraphStyle setMinimumLineHeight:spacing];
[paragraphStyle setMaximumLineHeight:spacing];
[textView setDefaultParagraphStyle:paragraphStyle]

But I get this error in the debugger:

-[NSSortDescriptor setLineSpacing:]: unrecognized selector sent to instance 0x1b202a0


________________________________
From: Graham Cox <graha...@bigpond.com>
To: Joshua Garnham <joshua....@yahoo.co.uk>
Cc: Cocoa-Dev List <coco...@lists.apple.com>
Sent: Fri, 1 January, 2010 0:16:04


Subject: Re: Setting the Line Height/ Line Spacing in an NSTextView.

Graham Cox

unread,
Jan 1, 2010, 4:16:36 AM1/1/10
to Joshua Garnham, coco...@lists.apple.com

On 01/01/2010, at 7:10 PM, Joshua Garnham wrote:

> That was a spelling mistake, it was meant to say NSMutableParagraphStyle.
> I've tried doing this …
>
> CGFloat spacing = 5.0f;
> NSMutableParagraphStyle *paragraphStyle;
> [paragraphStyle init];


Well, this is just not how you allocate objects. In fact, you haven't allocated an object - you are calling -init on a piece of random memory. Usually that will crash but in this case it just so happens that the variable, by pure chance, happens to point to memory that contains a NSSortDescriptor which is rejecting the -setLineSpacing method call.

You need to do this:

NSMutableParagraphStyle* ps = [[NSMutableParagraphStyle alloc] init];

This is Cocoa 101. If you don't know about this, go back and make sure you read the most fundamental documentation. You won't get far without knowing how to correctly alloc/init objects, so fiddling with paragraph styles before you can alloc/init is definitely a case of running before you can walk.

Joshua Garnham

unread,
Jan 1, 2010, 7:35:59 AM1/1/10
to Graham Cox, coco...@lists.apple.com
Oh, I see. That works partly. The text that is already in the NSTextView at launch has the new Line Height but as soon as you start to type the line your typing on resets it's line height to default.


________________________________
From: Graham Cox <graha...@bigpond.com>
To: Joshua Garnham <joshua....@yahoo.co.uk>

Cc: coco...@lists.apple.com
Sent: Fri, 1 January, 2010 9:16:36


Subject: Re: Setting the Line Height/ Line Spacing in an NSTextView.

Paul Sanders

unread,
Jan 1, 2010, 10:09:47 AM1/1/10
to Joshua Garnham, Graham Cox, coco...@lists.apple.com
Check out NSTextView's setTypingAttributes: method. And look up
the meaning of NSParagraphStyleAttributeName. It's really not
so hard...

Paul Sanders.

----- Original Message -----
From: "Joshua Garnham" <joshua....@yahoo.co.uk>
To: "Graham Cox" <graha...@bigpond.com>
Cc: <coco...@lists.apple.com>
Sent: Friday, January 01, 2010 12:35 PM
Subject: Re: Setting the Line Height/ Line Spacing in an
NSTextView.

Oh, I see. That works partly. The text that is already in the
NSTextView at launch has the new Line Height but as soon as you
start to type the line your typing on resets it's line height to
default.

_______________________________________________

Joshua Garnham

unread,
Jan 2, 2010, 2:57:16 AM1/2/10
to Paul Sanders, coco...@lists.apple.com
How would I use that?


________________________________
From: Paul Sanders <p.sa...@alpinesoft.co.uk>
To: Joshua Garnham <joshua....@yahoo.co.uk>; Graham Cox <graha...@bigpond.com>
Cc: coco...@lists.apple.com
Sent: Fri, 1 January, 2010 15:09:47


Subject: Re: Setting the Line Height/ Line Spacing in an NSTextView.

Check out NSTextView's setTypingAttributes: method. And look up

Graham Cox

unread,
Jan 2, 2010, 5:13:58 AM1/2/10
to Joshua Garnham, Cocoa-Dev List

On 02/01/2010, at 6:57 PM, Joshua Garnham wrote:

> How would I use that?


RTFM:

http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/TextArchitecture/TextArchitecture.pdf

--Graham

Paul Sanders

unread,
Jan 2, 2010, 6:04:52 AM1/2/10
to Graham Cox, Joshua Garnham, Cocoa-Dev List
Well, I have to say Graham has a point there, and I'm not going
to write your code for you, but what I do goes something like
this:

1. Get the current typing attributes from my NSTextView (as an
NSDictionary)

2. Make a mutable copy (as an NSMutableDictionary)

3. Retrieve the current NSParagrahpStyle from the dictionary
(using NSParagraphStyleAttributeName as a key)

4. Make a mutable copy of that (as an NSMutableParagraphStyle)

5. Adjust this as desired (refer to docs)

6. Add it to the NSMutableDictionary (again with key
NSParagraphStyleAttributeName)

7. Pass the dictionary to NSTextView's setTypingAttributes
method

8. Release your mutable copies

You can do something similar (using NSFont and
NSFontAttributeName) if you want to change the font (which might
be what you're after, I'm not sure).

If none of this makes any sense to you, you need to do some
background reading (it's not something you can do in 5 minutes)
and then write youself a little test program. Mine is called
Cocoa Testbench, and it taught me a lot.

As an aside, I remember when I first started learning Cocoa how
tiring I found it, mentally (I am not the youngest of spring
chickens). There is a lot to know. I think it's Hillegass who
recommends getting plenty of sleep. His book "Cocoa Programming
for Mac OS X" might be worth buying (that will be $5 please
Aaron :).

Paul Sanders

----- Original Message -----
From: "Graham Cox" <graha...@bigpond.com>
To: "Joshua Garnham" <joshua....@yahoo.co.uk>

Cc: "Cocoa-Dev List" <coco...@lists.apple.com>
Sent: Saturday, January 02, 2010 10:13 AM
Subject: Re: Setting the Line Height/ Line Spacing in an
NSTextView.

On 02/01/2010, at 6:57 PM, Joshua Garnham wrote:

> How would I use that?


RTFM:

http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/TextArchitecture/TextArchitecture.pdf

--Graham


_______________________________________________

Cocoa-dev mailing list (Coco...@lists.apple.com)

Please do not post admin requests or moderator comments to the
list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/p.sanders%40alpinesoft.co.uk

This email sent to p.sa...@alpinesoft.co.uk

Andy Lee

unread,
Jan 2, 2010, 7:45:38 AM1/2/10
to Paul Sanders, Cocoa-Dev List, Joshua Garnham
On Jan 2, 2010, at 6:04 AM, Paul Sanders wrote:
> I think it's Hillegass who
> recommends getting plenty of sleep.

Ten hours! I keep meaning to try that suggestion, I think there must be something to it.

--Andy

Joshua Garnham

unread,
Jan 2, 2010, 9:52:09 AM1/2/10
to Paul Sanders, coco...@lists.apple.com
Thanks for the Guide/Walkthrough, Here's what I have come up with from that:

NSDictionary *tA = [textView typingAttributes];
NSMutableDictionary *tAM = [tA mutableCopy];
NSParagraphStyle *pS = [tAM objectForKey:NSParagraphStyleAttributeName];
NSMutableParagraphStyle *pSM = [pS mutableCopy];
[pSM setMinimumLineHeight:spacing];
[pSM setMaximumLineHeight:spacing];
[tAM setObject:pSM forKey:NSParagraphStyleAttributeName];
[textView setTypingAttributes:tAM];
[tAM release];
[pSM release];

All of it seems ok in XCode but when I run it I get an error in the debugger saying

*** -[NSCFDictionary setObject:forKey:]: attempt to insert nil value (key: NSParagraphStyle)

It seems to be saying that the NSMutableParagraphStyle is empty. I'll take a look at Aaron's Book, Thanks.


Josh


________________________________
From: Paul Sanders <p.sa...@alpinesoft.co.uk>

To: Graham Cox <graha...@bigpond.com>; Joshua Garnham <joshua.garnham@yahoo..co.uk>
Cc: Cocoa-Dev List <coco...@lists.apple.com>
Sent: Sat, 2 January, 2010 11:04:52

Paul Sanders

unread,
Jan 2, 2010, 10:03:11 AM1/2/10
to Joshua Garnham, coco...@lists.apple.com
Yep, that looks ok. I think pS is probably being returned as nil. You can check this in Xcode. In which case, you can add:

if (pS == nil)
pS = [NSParagraphStyle defaultParagraphStyle];

Paul Sanders.

----- Original Message -----
From: Joshua Garnham
To: Paul Sanders
Cc: coco...@lists.apple.com
Sent: Saturday, January 02, 2010 2:52 PM
Subject: Re: Setting the Line Height/ Line Spacing in an NSTextView.

Thanks for the Guide/Walkthrough, Here's what I have come up with from that:

NSDictionary *tA = [textView typingAttributes];
NSMutableDictionary *tAM = [tA mutableCopy];
NSParagraphStyle *pS = [tAM objectForKey:NSParagraphStyleAttributeName];
NSMutableParagraphStyle *pSM = [pS mutableCopy];
[pSM setMinimumLineHeight:spacing];
[pSM setMaximumLineHeight:spacing];
[tAM setObject:pSM forKey:NSParagraphStyleAttributeName];
[textView setTypingAttributes:tAM];
[tAM release];
[pSM release];

All of it seems ok in XCode but when I run it I get an error in the debugger saying

*** -[NSCFDictionary setObject:forKey:]: attempt to insert nil value (key: NSParagraphStyle)

It seems to be saying that the NSMutableParagraphStyle is empty. I'll take a look at Aaron's Book, Thanks.


Josh


--------------------------------------------------------------------------------
From: Paul Sanders <p.sa...@alpinesoft.co.uk>
To: Graham Cox <graha...@bigpond.com>; Joshua Garnham <joshua....@yahoo.co.uk>

Joshua Garnham

unread,
Jan 2, 2010, 1:15:47 PM1/2/10
to Paul Sanders, coco...@lists.apple.com
Thanks that works now!


________________________________
From: Paul Sanders <p.sa...@alpinesoft.co.uk>
To: Joshua Garnham <joshua....@yahoo.co.uk>
Cc: coco...@lists.apple.com
Sent: Sat, 2 January, 2010 15:03:11


Subject: Re: Setting the Line Height/ Line Spacing in an NSTextView.




Josh


________________________________

Reply all
Reply to author
Forward
0 new messages