【问题标题】:Preserve ` ` in NSString?在 NSString 中保留` `?
【发布时间】:2009-08-08 20:19:52
【问题描述】:

我想在 iPhone 应用程序文本字段中保留   的条目。然而,记录和随后查看 UItextField 表明这个字符串正在变成一个普通的旧空间......

当我在文本字段中添加&nbsp 时,它会作为单个字符直到最后一个分号,此时它会从日志消息中消失并被视为一个简单的空格,因此...

MyApp[17425:20b] 记录字符串 &nbsp1234

//我这里加分号...

MyApp[17425:20b] 记录字符串 1234

我想要做的是防止 &nbsp 被自动转换 - 以便以后可以将其发送到网站并以 HTML 格式查看。我什至可以用 NSString 做到这一点还是需要别的东西?

编辑:我还应该说,在我将数据保存到数组并从数组中重新填充 UITextField 之后,我更希望 UITextField 也显示这个...我想这一切都归结为是否NSString 真的在转换它,或者它只是隐藏它......我猜我不清楚是什么自动神奇地将&nbsp 转换为 NSString 的空间(至少用于显示)......

编辑 2:2009 年 8 月 10 日抱歉,如果这是不正确的用法,我无法获得允许我发布“答案”的界面,也无法添加评论...

在回答马特的问题时,我将在另一个编辑中发布一些代码......另外有趣的是在字符串包含 &nbsp 和它包含   之间记录字符串的“计数”...我发现有趣的是,当我通过输入分号“完成”  时,计数似乎减少了......最初这导致了以下错误,因为(我认为)字符串的计数和范围的计数不同只要  完成(我会稍后发布代码)... -[NSCFString substringWithRange:]: Range or index out of bounds'

我通过在代码中使用字符串计数而不是范围计数来修复崩溃错误,但它仍然很有趣,并且“文字”  不再显示在文本字段中...

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

2009-08-08 14:58:26.320 [17897:20b] 这是 str 的计数 27

//此时我输入分号,字符串数回落……

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

2009-08-08 14:58:28.096 [17897:20b] 这是 str 的计数 23

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

2009-08-08 14:58:31.223 [17897:20b] 这是 str 的计数 24

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

2009-08-08 14:58:31.320 [17897:20b] 这是 str 的计数 25

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

2009-08-08 14:58:31.423 [17897:20b] 这是 str 的计数 26

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

2009-08-08 14:58:31.527 [17897:20b] 这是 str 的计数 27

编辑 4 8/10/09:发布上面的代码,因为否则它不会显示...

谢谢马特,确实很有趣...当我发布我的 cmets 时,我突然想到这可能是 TextView 的问题——我尝试在 IB 中更改几个设置,但在“消失”方面没有任何改变"   感谢您的宝贵时间!

编辑 3:2009 年 8 月 10 日

- (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];
    }
}

【问题讨论】:

  • 哇。这很奇怪。我能够复制您遇到的问题,但这不是 NSString 的问题。您可以通过创建一个像这样的 NSString *hello = @"Hello World"; 来验证这一点。然后用 NSLog 打印出来。不,这与 UITextView 的“文本”属性有关。你可能想在这个上提交一份雷达。也许有一个解释,但我还没有找到它。会继续看,因为这个很有趣。
  • 我无法用这个解决任何其他问题。我尝试了其他 HTML 代码,它们都打印得很好。它似乎只是  我会提交一个错误:developer.apple.com/BugReporter -Matt
  • 感谢您观看马特...很高兴知道我没有想象它!

标签: iphone html nsstring


【解决方案1】:

你想要 

应该是一样的。

【讨论】:

  • 谢谢 - 好吧,我想两者都行 - 但我需要用户能够查看和验证 - 他们已经习惯了 &amp;nbsp; 或者你的意思是在代码中使用它会显示 @ 987654322@ 在 UITextField 中?
  • 啊,我想我明白了 - 使用   可以使它在发送到网站时作为 html 工作,是吗?
【解决方案2】:

这很容易验证,但是 NSString 不会自动转换你的 HTML 标签。您的代码是什么样子导致了这个问题? NSString 有 -stringByAddingPercentEscapesUsingEncoding: 但它只是对字符串进行 URL 编码。看起来很奇怪,但必须有一个解释。你给我们看一些代码怎么样。 :-D

【讨论】:

    猜你喜欢
    • 2019-01-01
    • 2015-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多