如何将HTML代码更改为符号iphone

时间:2011-04-28 06:37:30

标签: iphone html ios symbols

我编写了一个函数,将“›”,“«”等文本更改为符号“>”“«”,我希望与stackoverflow用户共享此函数。如果您有如何使这个功能更好的建议请写!!!谢谢!!!

- (NSString*) ChangeAccentsLettersToSymbols: (NSString*) strToCorrect {
    NSLog(@"ChangeAccentsLettersToSymbols Entered\n");
    static NSString * const codeMap[][2] = {
        {@"¡",    @"¡"},  {@"«",    @"«"},  {@"»",    @"»"},  {@"‹",   @"‹"},  
        {@"›",   @"›"},  {@"‚",    @"‚"},  {@"„",    @"„"},  {@"“",    @"“"},  
        {@"”",    @"”"},  {@"‘",    @"‘"},  {@"’",    @"’"},  {@"¢",     @"¢"},
        {@"£",    @"£"},  {@"¥",      @"¥"},  {@"€",     @"€"},  {@"¤",   @"¤"},  
        {@"&fnof;",     @"ƒ"},  {@"&gt;",       @">"},  {@"&lt;",       @"<"},  {@"&divide;",   @"÷"},  
        {@"&deg;",      @"°"},  {@"&not;",      @"¬"},  {@"&plusmn;",   @"±"},  {@"&micro;",    @"µ"},
        {@"&amp;",      @"&"},  {@"&reg;",      @"®"},  {@"&copy;",     @"©"},  {@"&trade;",    @"™"},  
        {@"&bull;",     @"•"},  {@"&middot;",   @"·"},  {@"&sect;",     @"§"},  {@"&ndash;",    @"–"},  
        {@"&mdash;",    @"—"},  {@"&dagger;",   @"†"},  {@"&Dagger;",   @"‡"},  {@"&loz;",      @"◊"},
        {@"&uarr;",     @"↑"},  {@"&darr;",     @"↓"},  {@"&larr;",     @"←"},  {@"&rarr;",     @"→"},  
        {@"&harr;",     @"↔"},  {@"&iquest;",   @"¿"},  {@"&nbsp;",     @" "},  {@"&quot;",     @"\""}
    };
    int count = sizeof(codeMap)/sizeof(codeMap[0]);
    for( int i=0; i<count; ++i ) {
        strToCorrect = [ strToCorrect stringByReplacingOccurrencesOfString: codeMap[i][0] 
                                                                withString: codeMap[i][1] ];
    }

    for( int i=33; i<126; ++i) {
        NSString* whotToReplace = [NSString stringWithFormat:@"&#%d;", i];
        NSString* replaceWith   = [NSString stringWithFormat:@"%c", (char*)i ];
        strToCorrect = [strToCorrect stringByReplacingOccurrencesOfString: whotToReplace
                                                               withString: replaceWith ];
    }
    return strToCorrect;
}

1 个答案:

答案 0 :(得分:2)

github上有回购 -

https://github.com/mwaterfall/MWFeedParser

看看这个功能

- (NSString *)stringByDecodingHTMLEntities;
- (NSString *)stringByEncodingHTMLEntities;

它对我来说很好。