views:

509

answers:

2

I want to preserve the entry of   in an iPhone application text field. However, logging and subsequent viewing of the UItextField suggest this string is getting turned into a plain old space...

When I add &nbsp to the text field, it goes as single characters until the final semicolon, at which point it disappears from the log message and is seen as simply a single space, thus...

MyApp[17425:20b] Log the string &nbsp1234

//I add the semicolon here...

MyApp[17425:20b] Log the string 1234

What I want to do is prevent &nbsp from being converted automatically - so that it can later be sent to a website and seen as HTML. Can I even do that with NSString or will it require something else?

Edit: I should have also said that I'd prefer for the UITextField to also display this after I save the data to an array and re-populate the UITextField from the array... I guess it all boils down to whether NSString is really converting this or if it just hides it... Guess I'm not clear on what goes on to auto-magically turn &nbsp into a space (at least for display) with NSString...

Edit 2: 8/10/09 Apologies if this is incorrect usage, I couldn't get the interface to allow me to post an "answer" nor could I add a comment...

In answer to Matt's question I will post some code, inside another edit... Also of interest is the logging of the "count" of the string between when the string contained &nbsp and when it contained  ... What I found interesting is that it seems the count decreases when I "complete"   by entering the semicolon... Originally this resulted in the following error because (I think) the count of the string and the count of the range differed as soon as   is completed (I'll post code in a bit)... -[NSCFString substringWithRange:]: Range or index out of bounds'

I fixed the crash error by using the string count in the code instead of the range count, but it's still interesting and the "literal"   is no longer displayed in the text field...

2009-08-08 14:58:26.319 [17897:20b] this is str::--> <p class="blankline"> &nbsp

2009-08-08 14:58:26.320 [17897:20b] this is str's count 27

//at this point I enter the semicolon and the string count drops back...

2009-08-08 14:58:28.095 [17897:20b] this is str::--> <p class="blankline">

2009-08-08 14:58:28.096 [17897:20b] this is str's count 23

2009-08-08 14:58:31.223 [17897:20b] this is str::--> <p class="blankline"> a

2009-08-08 14:58:31.223 [17897:20b] this is str's count 24

2009-08-08 14:58:31.319 [17897:20b] this is str::--> <p class="blankline"> as

2009-08-08 14:58:31.320 [17897:20b] this is str's count 25

2009-08-08 14:58:31.423 [17897:20b] this is str::--> <p class="blankline"> asd

2009-08-08 14:58:31.423 [17897:20b] this is str's count 26

2009-08-08 14:58:31.527 [17897:20b] this is str::--> <p class="blankline"> asdf

2009-08-08 14:58:31.527 [17897:20b] this is str's count 27

Edit 4 8/10/09 : posted above code because it wouldn't show up otherwise...

Thanks Matt, interesting indeed... as I was posting my comments it occurred to me that it might be an issue with TextView -- I tried changing a couple of settings in IB, but nothing changed in terms of the "disappearing" &nbsp; My thanks for your time!

Edit 3: 8/10/09

- (void)textViewDidChange:(UITextView *)aTextView {
    [self updateTextViewPlacehoderFieldStatus];

    if (dismiss == YES) {
        dismiss = NO;
        return;
    }

    NSRange range = [aTextView selectedRange];
    NSArray *stringArray = [NSArray arrayWithObjects:@"http:", @"ftp:", @"https:", @"www.", nil];

    NSString *str = [aTextView text];
    NSLog(@"this is str::-->  %@", str);
    NSLog(@"this is str's count %d", str.length);
//as soon as I enter the semicolon, the printout of this log message displays a single space and the count decreases...
    int i, j, count = [stringArray count];
    BOOL searchRes = NO;

    for (j = 4; j <= 6; j++) {
        if (range.location < j)
            return;

        NSRange subStrRange;
       // subStrRange.location = range.location - j;
     //I took this out because adding &nbsp; to the post caused a mismatch between the length of the string from the text field and range.location
     //both should be equal, but my best guess is that the OS/Cocoa interprets &nbsp; as ONE space.
     //This caused NSString *subStr = [str substringWithRange:subStrRange]; to fail if the user entered &nbsp; in the text field
     subStrRange.location = str.length -j;
        subStrRange.length = j;
        [self setSelectedLinkRange:subStrRange];

       NSString *subStr = [str substringWithRange:subStrRange];
//Code crashed here with error -[NSCFString substringWithRange:]: Range or index out of bounds'
//I fixed this by using str.length instead of range.location

        for (i = 0; i < count; i++) {
            NSString *searchString = [stringArray objectAtIndex:i];

            if (searchRes = [subStr isEqualToString:[searchString capitalizedString]])
                break;else if (searchRes = [subStr isEqualToString:[searchString lowercaseString]])
                break;else if (searchRes = [subStr isEqualToString:[searchString uppercaseString]])
                break;
        }

        if (searchRes)
            break;
    }

    if (searchRes && dismiss != YES) {
        [textView resignFirstResponder];
        UIAlertView *linkAlert = [[UIAlertView alloc] initWithTitle:@"Link Creation" message:@"Do you want to create link?" delegate:self cancelButtonTitle:@"Create Link" otherButtonTitles:@"Dismiss", nil];
        [linkAlert setTag:1];  // for UIAlertView Delegate to handle which view is popped.
        [linkAlert show];
        MyAppAppDelegate *delegate = [[UIApplication sharedApplication] delegate];
        [delegate setAlertRunning:YES];
        [linkAlert release];
    }
}
A: 

do you want &#160;

which should be the same thing.

scunliffe
Ahh, I think I get you - using would make it work as html when it gets sent to the website, yes?
A: 

This is pretty easy to verify, but NSString will not automatically convert your HTML tags. What does your code look like that is causing this issue? NSString has -stringByAddingPercentEscapesUsingEncoding: but it just URL encodes the string. Seems quite strange, but there has to be an explanation. How about you show us some code. :-D

Matt Long