Re: Getting copy and paste with custom text attributes to work
Re: Getting copy and paste with custom text attributes to work
- Subject: Re: Getting copy and paste with custom text attributes to work
- From: Todd Ransom <email@hidden>
- Date: Thu, 13 Oct 2005 17:36:53 -0600
Subclass NSTextView and override these:
#pragma mark Pasteboard
- (NSArray *)writablePasteboardTypes;
- (BOOL)writeSelectionToPasteboard:(NSPasteboard *)pboard type:
(NSString *)type;
- (NSArray *)readablePasteboardTypes;
- (BOOL)readSelectionFromPasteboard:(NSPasteboard *)pboard type:
(NSString *)type;
Todd Ransom
Return Self Software
http://returnself.com
On Oct 12, 2005, at 8:07 PM, Willie Alberty wrote:
After struggling with this all day, I'm pretty sure that I just
keep glossing over the right section of the documentation. I'd
appreciate a pointer in the right direction...
I am adding some custom text attributes to the characters in a
particular NSTextStorage. These attributes will cause special
drawing to happen at some point in the future. Manipulating the
attributes programmatically is working perfectly. However, as soon
as I copy and paste in the NSTextView, the custom attributes are
lost in the pasted text. Short of subclassing NSTextView and
creating a custom pasteboard type, I cannot figure out how to keep
the attributes attached to the text. (And I'm fine with doing that
if need be; just wanted to make sure that's the right approach.)
Here's a simplified example. The nib file has a single NSTextView
connected to the textView outlet and a single NSButton whose action
is attached to the logAttributeType action in the AppController
object. After waking, the text storage contains a string with the
custom attribute attached to the second word. Clicking the button
will log the attribute's value at the text insertion point. If you
copy and paste the text, none of the pasted versions will contain
the custom attribute:
// AppController.h
#import <Cocoa/Cocoa.h>
@interface AppController : NSObject {
IBOutlet NSTextView *textView;
}
- (void)awakeFromNib;
- (IBAction)logAttributeType:(id)sender;
@end
// AppController.m
#import "AppController.h"
@implementation AppController
- (void)awakeFromNib;
{
NSMutableAttributedString *theString;
theString = [[NSMutableAttributedString alloc]
initWithString:@"This word has attributes."];
[theString addAttribute:@"myAttribute" value:@"myValue"
range:NSMakeRange(5,4)];
[[textView textStorage] setAttributedString:theString];
[theString release];
}
- (IBAction)logAttributeType:(id)sender;
{
unsigned length = [[textView textStorage] length];
if (length == 0) return;
NSRange selectedRange = [textView selectedRange];
if (selectedRange.location == length) return;
NSString *attributeValue;
attributeValue = [[textView textStorage]
attribute:@"myAttribute" atIndex:selectedRange.location
effectiveRange:NULL];
if (attributeValue == nil) {
NSLog(@"The attribute doesn't exist!");
} else {
NSLog(@"Attribute value: %@", attributeValue);
}
}
@end
--
Willie Alberty, Owner
Spenlen Media
email@hidden
http://www.spenlen.com/
_______________________________________________
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
_______________________________________________
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