Re: Make an attributed string all uppercase without losing attributes?
Re: Make an attributed string all uppercase without losing attributes?
- Subject: Re: Make an attributed string all uppercase without losing attributes?
- From: Graham Cox <email@hidden>
- Date: Sat, 9 May 2009 14:11:50 +1000
On 09/05/2009, at 1:11 PM, Jim Correia wrote:
The documentation for -[NSMutableString
replaceCharactersInRange:withString:] is perhaps more enlightening.
"""The new characters inherit the attributes of the first replaced
character from aRange"""
Processing the string by attribute runs sounds like it should have the
effect you are looking for.
OK, makes sense.
However, it's still not working quite how I'd expected, and there's
something odd. Here's my new code:
@implementation NSMutableAttributedString (CaseChange)
- (void) makeUppercase
{
NSRange effectiveRange = NSMakeRange( 0, 0 );
NSRange rangeLimit = NSMakeRange( 0, [self length]);
NSDictionary* attributes;
while( rangeLimit.length > 0 )
{
attributes = [self attributesAtIndex:rangeLimit.location
longestEffectiveRange:&effectiveRange inRange:rangeLimit];
NSString* str = [[[self string] substringWithRange:effectiveRange]
uppercaseString];
[self replaceCharactersInRange:effectiveRange withString:str];
NSLog(@"replacement range: %@, attributes = %@",
NSStringFromRange( effectiveRange ), attributes);
rangeLimit = NSMakeRange( NSMaxRange( effectiveRange ), [self
length] - NSMaxRange( effectiveRange ));
}
}
I have a string which I changed one four-letter word in the middle of
to have a number of different font and other traits - bold, italic,
larger than normal and underlined. After running the above, the
underline attribute is preserved but nothing else. Looking at what is
logged, something weird shows up (edited down to just the relevant
stuff):
2009-05-09 13:58:16.044 Ortelius[57577:10b] replacement range: {0,
16}, attributes = {
NSFont = "Helvetica 14.00 pt. P [] (0x1618ad80) fobj=0x00589eb0,
spc=3.89";
}
2009-05-09 13:58:16.047 Ortelius[57577:10b] replacement range: {16,
4}, attributes = {
NSFont = "Helvetica 14.00 pt. P [] (0x1618ad80) fobj=0x00589eb0,
spc=3.89";
NSOriginalFont = "Helvetica-BoldOblique 19.00 pt. P []
(0x161b1780) fobj=0x005a1590, spc=5.28";
NSUnderline = 1;
}
2009-05-09 13:58:16.049 Ortelius[57577:10b] replacement range: {20,
10}, attributes = {
NSFont = "Helvetica 14.00 pt. P [] (0x1618ad80) fobj=0x00589eb0,
spc=3.89";
}
The ranges detected are correct. The attributes detected are sort of
correct, except that the font traits for the modified word in the
middle are listed under 'NSOriginalFont', which is not something I can
find documented anywhere. These settings are right, but what actually
applies is the NSFont setting, which is not right for that range.
So what on earth is 'NSOriginalFont' and why is it showing up here? I
can see a solution but I'm loath to use something undocumented that
I'm not understanding.
--Graham
_______________________________________________
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