[英]Convert NSString with unicode characters into valid HTML
我從一個帶有錨標簽的API中獲取一個字符串,所以我NSAttributedString
創建一個NSAttributedString
,並在UITextView
顯示它,這樣我就可以支持可點擊的鏈接。
問題是傳入的字符串不是有效的HTML,因此它中沒有未轉義的unicode字符。 像:
雖然我可以處理這些特定情況,但我擔心任何其他的unicode字符,我目前還不知道。
例:
NSString *fromAPI = @"Reagan \U2014 saying";
NSDictionary *options = @{NSDocumentTypeDocumentAttribute : NSHTMLTextDocumentType};
NSData *data = [fromAPI dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:NO];
NSAttributedString *attributedString = [[NSAttributedString alloc] initWithData:data options:options documentAttributes:nil error:nil];
這在UITextView中呈現為:
如何讓它正確渲染em破折號和其他unicode?
找到它,看起來HTML不會渲染unicode,除非你將它添加到<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
我要建議的是(如果我已正確理解了這個問題)是使用正則表達式或其他東西將轉義字符\\U0000FE0E
或只是\\UFE0E
到所有未轉義的unicode字符的末尾,例如:
NSString *fromAPI = @"Reagan \U2014 saying";
NSString *convertedFromAPI = @"Reagan \U2014\UFE0E saying";
但我認為你現在所做的事情更有意義。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.