Re: NSTextField subclass needs cell subclass as well - how?
Re: NSTextField subclass needs cell subclass as well - how?
- Subject: Re: NSTextField subclass needs cell subclass as well - how?
- From: Charles Srstka <email@hidden>
- Date: Thu, 08 Oct 2015 01:38:26 -0500
> On Oct 7, 2015, at 11:08 PM, Graham Cox <email@hidden> wrote:
>
> I’ve tried swapping out the text field’s cell by creating a subclass and copying all of the original settings into it. Unfortunately that doesn’t work - just copying across the state isn’t enough to make the replacement cell look and act like the original text field cell, for some reason - presumably there’s some order-dependent setup or other hidden stuff in the ‘real’ text field cell that I can’t get at.
Did you try fully copying the state by using NSKeyedArchiver? That should work to replicate all the setup the cell would have done when unarchiving from the nib:
+ (nullable NSCell *)cellOfClass:(nonnull Class)cellClass byCloningCell:(nonnull NSCell *)cell {
if (![cellClass isSubclassOfClass:cell.class]) {
return nil;
}
NSMutableData *data = [NSMutableData new];
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
[cell encodeWithCoder:archiver];
[archiver finishEncoding];
NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
NSCell *newCell = [[cellClass alloc] initWithCoder:unarchiver];
[unarchiver finishDecoding];
return newCell;
}
Charles
_______________________________________________
Cocoa-dev mailing list (email@hidden)
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:
This email sent to email@hidden