I have a an NSAttributedString into which I would like to insert images.  The 
images need to be inserted after specific line endings (I know which line 
endings in advance - they've been previously calculated).

I have written a function which returns an array of all line ending locations 
(or anything else that you'd care to search for) for a given string:

+(NSArray*)getLocationsOfString:(NSString*)stringToFind 
inString:(NSString*)sourceString {
    NSMutableArray* returnArray = [[NSMutableArray alloc]init];

    @autoreleasepool
    {
        NSMutableString* sourceToSearch = [[NSMutableString 
alloc]initWithString:sourceString];
        unsigned long locationIterator=0;
        while ([sourceToSearch 
rangeOfString:stringToFind].location!=NSNotFound) {
            unsigned long foundLocation = [sourceToSearch 
rangeOfString:stringToFind].location+1;
            [sourceToSearch deleteCharactersInRange:NSMakeRange(0, 
foundLocation)];
            [returnArray addObject:[NSNumber 
numberWithUnsignedLong:foundLocation+locationIterator]];
            locationIterator+=foundLocation;
        }
    }
    return returnArray;
}

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 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?

Hoping that someone can help!








_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to