• 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
Getting copy and paste with custom text attributes to work
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Getting copy and paste with custom text attributes to work


  • Subject: Getting copy and paste with custom text attributes to work
  • From: Willie Alberty <email@hidden>
  • Date: Wed, 12 Oct 2005 19:07:59 -0700

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


  • Follow-Ups:
    • Re: Getting copy and paste with custom text attributes to work
      • From: Todd Ransom <email@hidden>
  • Prev by Date: Disk image won't open under pre-Panther
  • Next by Date: Re: Bindings and Core Data question.
  • Previous by thread: Re: Disk image won't open under pre-Panther
  • Next by thread: Re: Getting copy and paste with custom text attributes to work
  • Index(es):
    • Date
    • Thread