Re: NSTextView, style, and weird Core Data issue
Re: NSTextView, style, and weird Core Data issue
- Subject: Re: NSTextView, style, and weird Core Data issue
- From: Matt Neuburg <email@hidden>
- Date: Thu, 27 Jul 2006 16:30:11 -0700
- Thread-topic: NSTextView, style, and weird Core Data issue
On Thu, 27 Jul 2006 06:41:41 -0700, Matt Neuburg <email@hidden> said:
>On Thu, 27 Jul 2006 00:27:43 -0700, Martin Wierschin <email@hidden>
>said:
>>> The docs say: "Typing attributes are reset automatically whenever the
>>> selection changes." Well, the selection *is* changing. But the typing
>>> attributes are not being reset (at least, I would not describe this
>>> behaviour using the word "reset" - I would describe it as
>>> "remaining the
>>> same as before").
>>
>>It says "reset", but probably "recalculated" is a more appropriate
>>word. Also, I do not think the preservation of the typing attributes
>>when you clear the NSTextView's content is a bug. Consider the
>>following case:
>>
>>1. User enters some text.
>>2. Selects all and applies bold.
>>3. Changes their mind and deletes all the text.
>>4. Starts typing.
>>
>>In step 4 they most likely prefer that the new text be bold. This is
>>normally convenient, but you'll have to work around it, as a zero
>>length NSAttributedString has no attributes to apply to the NSTextView.
>
>Well, it's funny you should mention this case, since it's the very one I'm
>now having trouble with. Let's say, for purposes of argument, that I don't
>care what "they most likely prefer", and that I want to clear the typing
>attributes in exactly the situation you describe. I'm using the following
>code:
>
>- (void)textViewDidChangeSelection:(NSNotification *)aNotification {
> NSTextView* tv = [aNotification object];
> NSArray* ranges = [tv selectedRanges];
> if ([ranges count] == 1) {
> if (NSEqualRanges([[ranges lastObject] rangeValue], NSMakeRange(0,0))) {
> if (![[tv textStorage] length]) {
> // seems to handle the case of a new entry
> NSTextStorage* ts = [[[NSTextStorage alloc] init] autorelease];
> [ts setAttributedString: [[[NSAttributedString alloc]
>initWithString:@" "] autorelease]];
> NSDictionary* atts = [ts attributesAtIndex:0 effectiveRange:nil];
> [tv setTypingAttributes:atts];
> NSLog(@"%@", [tv typingAttributes]);
>
> }
> }
> }
>}
>
>So now let's say I start with an empty NSTextView. I press Command-U (for
>Underline) and type a few letters; sure enough, they are underlined. Now I
>hit Delete repeatedly until the NSTextView is empty. Sure enough, my code
>fires, and the following is logged:
>
>{NSFont = "Helvetica 12.00 pt. P [] (0x00398db0) fobj=0x00337070, spc=3.33";
>}
>
>However, when I now start typing again, the text is still underlined! How
>can this be? I am explicitly setting the typing attributes to be
>non-underlined, and that command is apparently being obeyed; so what other
>unseen force is struggling against me? Who's in charge here, anyway? Or,
>more practically, what command could I give that says, "I really mean it,
>clear the typing attributes!"
Well, I still don't know the answer to the first question ("what other
unseen force is struggling against me?"), but the answer to the last
question, it turns out (after an entire day of trial and error) is Delayed
Performance. Here's code that works:
- (void)textViewDidChangeSelection:(NSNotification *)aNotification {
NSTextView* tv = [aNotification object];
NSArray* ranges = [tv selectedRanges];
if ([ranges count] == 1) {
if (NSEqualRanges([[ranges lastObject] rangeValue], NSMakeRange(0,0))) {
if (![[tv textStorage] length]) {
[self performSelector:@selector(doYourThing:)
withObject:tv afterDelay:0.1];
}
}
}
}
- (void) doYourThing: (id) tv {
NSTextStorage* ts = [[[NSTextStorage alloc] init] autorelease];
[ts setAttributedString:
[[[NSAttributedString alloc] initWithString:@" "] autorelease]];
NSDictionary* atts = [ts attributesAtIndex:0 effectiveRange:nil];
[tv setTypingAttributes:atts];
}
m.
--
matt neuburg, phd = email@hidden, <http://www.tidbits.com/matt/>
A fool + a tool + an autorelease pool = cool!
AppleScript: the Definitive Guide - Second Edition!
<http://www.amazon.com/gp/product/0596102119>
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden