Objective-C:如何替换 HTML 实体?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2364797/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 02:22:23  来源:igfitidea点击:

Objective-C: How to replace HTML entities?

iphonehtmlobjective-cspecial-characters

提问by arielcamus

I'm getting text from Internet and it contains html entities (i.e. ó= ó). I want to show this text into a custom iPhone cell.

我从 Internet 获取文本,它包含 html 实体(即ó= ó)。我想将此文本显示到自定义 iPhone 单元格中。

I've tried to use a UIWebView into my custom cell but I prefer to use a multiline UILabel. The problem is I can't find any way of replacing these HTML entities.

我尝试在我的自定义单元格中使用 UIWebView,但我更喜欢使用多行 UILabel。问题是我找不到任何替换这些 HTML 实体的方法。

回答by Michael Waterfall

Check out my NSString category for HTML. Here are the methods available:

查看我的NSString 类别以获取 HTML。以下是可用的方法:

- (NSString *)stringByConvertingHTMLToPlainText;
- (NSString *)stringByDecodingHTMLEntities;
- (NSString *)stringByEncodingHTMLEntities;
- (NSString *)stringWithNewLinesAsBRs;
- (NSString *)stringByRemovingNewLinesAndWhitespace;

回答by Matt Stevens

Google Toolbox for Macincludes an iPhone-compatible NSString addition that will do this for you: gtm_stringByUnescapingFromHTMLdefined in GTMNSString+HTML.hand GTMNSString+HTML.m. If you comment out the calls to _GTMDevLog and #import "GTMDefines.h" in the .m you only need to add these two files to your project.

Google Toolbox for Mac包含一个与 iPhone 兼容的 NSString 附加项,可以为您执行此操作:gtm_stringByUnescapingFromHTMLGTMNSString+HTML.hGTMNSString+HTML.m 中定义。如果您在 .m 中注释掉对 _GTMDevLog 和 #import "GTMDefines.h" 的调用,您只需要将这两个文件添加到您的项目中。

回答by Warewolf

You can make a method that can replace html entities with strings given by you.

您可以制作一种可以用您提供的字符串替换 html 实体的方法。

+(NSString*)parseString:(NSString*)str
{
    str  = [str stringByReplacingOccurrencesOfString:@"–" withString:@"-"];
    str  = [str stringByReplacingOccurrencesOfString:@"”" withString:@"\""];          
    str  = [str stringByReplacingOccurrencesOfString:@"“" withString:@"\""];          
    str  = [str stringByReplacingOccurrencesOfString:@"ó" withString:@"o"];          
    str  = [str stringByReplacingOccurrencesOfString:@"'" withString:@"'"];                
    return str;
}

call this method to replace string by sending string as parameter.

调用此方法通过发送字符串作为参数来替换字符串。

回答by dmaclach

To expand on Matt Stevens answer (since I'm not allowed to comment yet), you don't need to comment out _GTMDevLog, as it is intentionally set up so that you can #define it yourself to whatever you want.

为了扩展马特史蒂文斯的答案(因为我还不允许发表评论),您不需要注释掉 _GTMDevLog,因为它是有意设置的,以便您可以根据需要自行#define。

回答by MarkPowell

Can you just use NSMutableString's replaceOccurrencesOfString:withString:options:range:method?

你能用NSMutableString's 的replaceOccurrencesOfString:withString:options:range:方法吗?