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
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
> 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
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?
> 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:._______________________________________________
> 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
________________________________
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.
> 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
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:
Why don't you tell us?
--Andy
> 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
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.
> 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.
________________________________
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.
----- 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.
_______________________________________________
________________________________
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
> How would I use that?
RTFM:
--Graham
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:
--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
Ten hours! I keep meaning to try that suggestion, I think there must be something to it.
--Andy
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
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>
________________________________
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
________________________________