• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Easy(?) Cocoa Question
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Easy(?) Cocoa Question


  • Subject: Re: Easy(?) Cocoa Question
  • From: Wade Tregaskis <email@hidden>
  • Date: Sat, 1 Nov 2003 11:54:13 +1100

This is what I can gather as one way of doing what you want - hopefully it helps you out. I've presumed you're going to extend NSTextView via a category, although you could rewrite this as functional methods if you prefer.

static NSString *timeAddedToField = @"Time Added To Text Field";

- (void)appendTimeStampedString:(NSString*)theString {
NSMutableAttributedString *newString = [[[NSAttributedString alloc] initWithString:theString] autorelease];

[newString addAttribute:timeAddedToField value:[NSDate date] range:NSMakeRange(0, [theString length])];

[[self textStorage] appendAttributedString:newString];
}

- (NSDate*)timeStampAtPosition:(unsigned int)position {
return [[self textStorage] attribute:timeAddedToField atIndex:position effectiveRange:nil];
}

Of course, the getter method may not be sufficient for your use, if you don't have any easy way of working out the position as an offset from the start of the field. But if you provide a delegate for the field, you can have the delegate implement the following method:

- (NSString*)textView:(NSTextView*)textView willDisplayToolTip:(NSString*)tooltip forCharacterAtIndex:(unsigned)characterIndex {
return [[textView timeStampAtPosition:characterIndex] descriptionWithCalendarFormat:@"%a %m/%d/%y %I:%M %p" timeZone:nil locale:nil];
}

It'd be nice if it said "5 minutes ago" or something instead of giving the actual date & time, or at least said something like that in brackets (e.g. "7 minutes ago (Mon 24/5/03 3:45 PM)"), but I can't figure out how to get a natural language string back out of an NSDate/NSCalendarDate.

Also note that this delegate method is only available in Panther onwards, so for 10.2 compatibility you'll have to find another method. I guess you'd start by using the characterIndexForPoint method of the NSTextView (as part of the NSTextInput protocol), but you'll still have to find a way of managing the display of the tooltips themselves; there doesn't appear to be an automatic mechanism in 10.2 like there is in Panther.

If you do create something from this, I hope you release it as free source - it'd be great to see this functionality in lots of other programs. :)

Wade Tregaskis
-- Sed quis custodiet ipsos custodes?

P.S. To be explicit, all the source I've provided in this email may be used freely without restriction, akin to being BSD licensed.
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.
  • Prev by Date: Re: Detecting login/logout?
  • Next by Date: Re: Detecting login/logout?
  • Previous by thread: Re: Detecting login/logout?
  • Next by thread: Re: Freelance programming
  • Index(es):
    • Date
    • Thread