Re: NSTextAttachment affecting NSMutableAttributedString attributes
Re: NSTextAttachment affecting NSMutableAttributedString attributes
- Subject: Re: NSTextAttachment affecting NSMutableAttributedString attributes
- From: Keith Blount <email@hidden>
- Date: Tue, 15 Nov 2005 11:04:20 -0800 (PST)
I believe this is a known "bug" in the text system. As
I understand it, NSMutableAttributedString's
-fixAttachmentAttributeInRange: does more than it is
documented to do - so that it strips *all* text
attributes, not just the attachment attributes. This
is really annoying for precisely the reasons you
detail - if an attachment is inserted at the end of a
text view, for instance, all typing attributes that
follow will be reset to the default Helvetica 12
point.
I learned this after finding the following post from
one of the (very helpful) Omni guys, who also offers a
solution:
http://www.cocoabuilder.com/archive/message/cocoa/2005/8/12/144201
The code there is rather specific to Omni, but it
gives you everything you need. You basically have to
subclass NSTextStorage and roll your own
implementation of -fixAttachmentAttributeInRange: so
that it *only* does what is documented and nothing
else (ie. strip attachment attributes only). My own
implementation - which is based on the Omni code but
refactored for use in a generic NSTextStorage subclass
- is as follows:
- (void)fixAttachmentAttributeInRange:(NSRange)aRange;
{
NSString *string = [self string];
NSRange effectiveRange;
id attributeValue;
while (aRange.length > 0)
{
attributeValue = [self
attribute:NSAttachmentAttributeName
atIndex:aRange.location
longestEffectiveRange:&effectiveRange
inRange:aRange];
if (attributeValue)
{
unsigned int charIndex;
for (charIndex = effectiveRange.location; charIndex
< NSMaxRange(effectiveRange); charIndex++)
{
if ([string characterAtIndex:charIndex] !=
NSAttachmentCharacter)
{
// Might need a -beginEditing message here if it
is not already...
[self removeAttribute:NSAttachmentAttributeName
range:NSMakeRange(charIndex, 1)];
}
}
}
aRange = NSMakeRange(NSMaxRange(effectiveRange),
NSMaxRange(aRange) -
NSMaxRange(effectiveRange));
}
}
If anyone has a better way, or can improve this code,
I would love to hear about it too.
HTH,
Keith
--- ORIGINAL MESSAGE ---
Hi.
Aside from my antialiasing problems mentioned in my
previous e-mail,
I'm also having some trouble with NSTextAttachment
messing up
attributes in an NSMutableAttributedString.
What I'm doing is taking an NSAttributedString,
replacing some of the
characters with other things, and then putting new
text in. In this
case, what I do is the user types some text, I take
it, do some
conversions, output to a PDF file, and then want to
attach that file
into the string. My initial solution, to just do a
replace with
aString as presented in the first statement below, but
that caused
all the attributes to disappear on the text typed
after where the
attachment was placed. I tried to fix this by
reapplying the
attributes to aString, but that failed, too. So I
took a trick I've
seen others do and added a space to get the formatting
to stick.
I have the following code as a work around:
aString = [NSMutableAttributedString
attributedStringWithAttachment:
[[[NSTextAttachment alloc]
initWithFileWrapper:
[[[NSFileWrapper alloc]
initWithPath: path]
autorelease]]
autorelease]
];
[aString beginEditing];
[aString appendAttributedString:
[[[NSAttributedString alloc] initWithString: @" "]
autorelease]];
[aString addAttribute:
NSFontAttributeName value:
[NSFont fontWithName: @"Palatino" size: 14] range:
NSMakeRange(0,
[aString length])];
[aString addAttribute:
NSForegroundColorAttributeName value: [NSColor
colorWithCalibratedRed: 0.2431 green: 0.1490 blue:
0.0784 alpha: 1]
range: NSMakeRange(0, [aString length])];
[aString addAttribute:
NSBackgroundColorAttributeName value: [NSColor
colorWithCalibratedRed: 1 green: 1 blue: 0.9607 alpha:
1] range:
NSMakeRange(0, [aString length])];
[aString endEditing];
(Obviously the stuff setting up colors and fonts
should be done
better; this is just a stop gap measure until I can
get a better
solution...I hope).
I'd really rather not have to insert that space there,
and this is an
ugly solution besides. Does anyone know how I can
achieve this
attachment insert without hosing the
NSAttributedString's attributes?
__________________________________
Yahoo! FareChase: Search multiple travel sites in one click.
http://farechase.yahoo.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