【问题标题】:How to add &nbsp;, &gt; and &lt; into text in UITextView?如何在 UITextView 的文本中添加 、 > 和 <?
【发布时间】:2011-03-17 12:55:21
【问题描述】:

我的应用需要添加字符串  , >和 <到 UITextView 中的文本中,以便我们可以在正确的位置替换字符串。我们知道 UITextView 会改变字符串  到空间,>到 > 和 <到 <. uitextview>

谢谢你。问候。

【问题讨论】:

    标签: objective-c ios uitextview character


    【解决方案1】:
    + (NSString *)extractTextFromXML:(NSString *)xml{
    //Will hold just the text
    NSMutableString *text = [NSMutableString string];
    NSInteger startOfSubstring = 0;
    //Finds first instance of "<"
    NSRange startTagRange = [xml rangeOfString:@"<"];
    while(startTagRange.location != NSNotFound){
        //Extracts text from last location up to "<"
        NSString *substring = [xml substringWithRange:NSMakeRange(startOfSubstring, startTagRange.location-startOfSubstring)];
        //Removes whitespace from substring
        [text appendString:[substring stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]];
    
        //Searches for ">" from "<" to end of string
        NSRange startTagToEndRange = NSMakeRange(startTagRange.location, [xml length]-startTagRange.location);
        NSRange endTagRange = [xml rangeOfString:@">" options:NSCaseInsensitiveSearch range:startTagToEndRange];
        //If ">" found, then sets next location of substring to after that
        if(endTagRange.location != NSNotFound){
            startOfSubstring = endTagRange.location+1;
        }
        //If no ">", then appends rest of string and returns
        else{
            [text appendString:[xml substringFromIndex:startTagRange.location]];
            return text;
        }
        //Finds next "<" in string
        NSRange endTagToEndRange = NSMakeRange(startOfSubstring, [xml length]-startOfSubstring);
        startTagRange = [xml rangeOfString:@"<" options:NSCaseInsensitiveSearch range:endTagToEndRange];
    }
    
    return text;
    

    }

    或者你可以使用 UIWebView 代替 UITextView

    【讨论】:

    • 感谢 makboney 的回答。但是,那不是我想要的。我的最后一个问题是错误的。我已经编辑过了。请再次阅读我的问题。 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-23
    • 1970-01-01
    • 2011-06-17
    • 1970-01-01
    相关资源
    最近更新 更多