Re: Differences in string handling for NSString and NSAttributedString
Re: Differences in string handling for NSString and NSAttributedString
- Subject: Re: Differences in string handling for NSString and NSAttributedString
- From: Pax <email@hidden>
- Date: Mon, 05 May 2014 17:50:16 +0100
Oh bloody hell. I must be tired. That's so obvious - and I'm desperately embarrassed for not taking care of this.
Thank you so much.
On 5 May 2014, at 17:47, Uli Kusterer <email@hidden> wrote:
>
> On 05 May 2014, at 18:27, Pax <email@hidden> wrote:
>> When I use this with a plain string it appears to work correctly. I can use it to chop up a string into constituent lines as follows (just a test - clearly there are better ways of achieving the same result):
>>
>> NSArray* newLineArray = [StringTools getLocationsOfString:@"\n" inString:[attributedRTFString string]];
>> int oldLoc = 0;
>> for (NSNumber* locNum in newLineArray) {
>> NSLog(@"%@",[[attributedRTFString string] substringWithRange:NSMakeRange(oldLoc, [locNum intValue]-oldLoc)]);
>> oldLoc = [locNum intValue];
>> }
>
> This code just splits the string, but doesn't insert anything.
>
>> This proves, to my satisfaction at least, that the line endings are being identified correctly and in the right place.
>>
>> When I try to use it with the attributed string, rather than the plain text representation of the attributed string, it goes wrong. In the interests of a quick test, I am trying to insert a string rather than an image.
>>
>> NSArray* newLineArray = [StringTools getLocationsOfString:@"\n" inString:[attributedRTFString string]];
>> if (([imageLocations count]>0)&&([newLineArray count]>0)) {
>> for (NSDictionary* imageDetails in imageLocations) {
>> unsigned long loc = [[imageDetails objectForKey:@"image location"]unsignedLongValue];
>> unsigned long locationForInsert = [[newLineArray objectAtIndex:loc]unsignedLongValue];
>> NSAttributedString* testString = [[NSAttributedString alloc]initWithString:@"IMAGE HERE" attributes:nil];
>> [attributedRTFString insertAttributedString:testString atIndex:locationForInsert];
>> }
>> }
>>
>> I believe that this should cause "IMAGE HERE" to be inserted at specific line endings. However, it often inserts into the middle of a paragraph instead - usually, but not always, close to where it should put the text. Furthermore, the positioning is usually at, or very close, to the correct location for the first instance of image insertion - and it gets progressively less accurate as it gets through the text. Is there some difference between the ways that strings are handled in NSAttributedString and NSString? Could there be some problem with characters like Non-Breaking Space?
>
> This actually changes the string by inserting something. The result of the insertion is that everything following that string is moved down by the length of the inserted string. You need to either adjust the offsets (by remembering how many characters you've inserted so far) or avoid touching the stuff after the insertion by going backwards over the string.
>
> Cheers,
> -- Uli Kusterer
> "The Witnesses of TeachText are everywhere..."
> http://www.zathras.de
>
_______________________________________________
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