我正在尝试为Mail.app提供一些简单的html:列表,粗体字体,一些斜体。但是,我注意到如果我使用像£
这样的字符,那么Mail.app就不会显示任何内容。我意识到我需要转换为HTML实体,例如£
(完整列表:http://www.w3schools.com/tags/ref_entities.asp)。我有一个部分解决方案适用于我的用户提出的大多数字符,但它远非一个坚实的解决方案:
- (NSString*) makeValidHTML:(NSString*)str {
str = [str stringByReplacingOccurrencesOfString:@"£" withString:@"£"];
str = [str stringByReplacingOccurrencesOfString:@"¢" withString:@"¢"];
str = [str stringByReplacingOccurrencesOfString:@"¥" withString:@"¥"];
str = [str stringByReplacingOccurrencesOfString:@"©" withString:@"©"];
str = [str stringByReplacingOccurrencesOfString:@"®" withString:@"®"];
str = [str stringByReplacingOccurrencesOfString:@"°" withString:@"°"];
str = [str stringByReplacingOccurrencesOfString:@"¿" withString:@"¿"];
str = [str stringByReplacingOccurrencesOfString:@"¡" withString:@"¡"];
str = [str stringByReplacingOccurrencesOfString:@"‘" withString:@"'"];
str = [str stringByReplacingOccurrencesOfString:@"'" withString:@"'"];
str = [str stringByReplacingOccurrencesOfString:@"&" withString:@"&"];
str = [str stringByReplacingOccurrencesOfString:@"\"" withString:@"""];
str = [str stringByReplacingOccurrencesOfString:@"“" withString:@"""];
str = [str stringByReplacingOccurrencesOfString:@"<" withString:@"<"];
str = [str stringByReplacingOccurrencesOfString:@">" withString:@">"];
return str;
}
有没有标准方法可以做到这一点,而无需列出所有可能的保留字符?
答案 0 :(得分:4)
这门课对你有帮助:
https://github.com/mwaterfall/MWFeedParser/blob/master/Classes/NSString+HTML.m
从其他SO回答中检索到的链接:
Converting & to & in Objective-C
答案 1 :(得分:0)
我认为您的主要问题是您没有编码并将您的HTML页面声明为UTF-8。虽然您提到的某些实体是真正的问题,需要转换,例如>
到>
(@Joel Martinez链接到的代码会对此有所帮助),例如{{1}如果页面被声明并编码为unicode格式(如UTF-8),则符号将正常工作:
£