如何添加,>和<在UITextView中的文本?

时间:2011-03-17 03:59:45

标签: objective-c ios uitextview character

我的应用需要添加字符串& nbsp;,& gt;和& lt;在UITextView中的文本,所以我们可以在正确的位置替换字符串。我们知道UITextView会改变字符串& nbsp;到太空,& gt;到>和& lt;到<。如果希望UITextView不将其更改为该字符或如何将该字符串添加到UITextView中,我们该怎么办?

谢谢你。问候。

1 个答案:

答案 0 :(得分:0)

+ (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