将attributetext转换为UTF8字符串?

时间:2018-03-30 12:13:01

标签: html ios objective-c

我从服务器获取\Ud835\Udc13\Ud835\Udc1e\Ud835\Udc2c\Ud835\Udc2d字符串。

当我使用NSMutableAttributedString转换

 NSMutableAttributedString *attribute = [[NSMutableAttributedString alloc] initWithData:[self.message dataUsingEncoding:NSUTF8StringEncoding] options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute: [NSNumber numberWithInt:NSUTF8StringEncoding]} documentAttributes:nil error:nil];

它在标签上显示完美的测试作为属性字符串。

我想使用UITexView将测试发送到服务器。

我正在使用以下代码

NSDictionary *documentAttributes = @{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute: [NSNumber numberWithInt:NSUTF8StringEncoding]};
    NSData *htmlData = [txtView.internalTextView.attributedText dataFromRange:NSMakeRange(0, txtView.internalTextView.attributedText.length) documentAttributes:documentAttributes error:NULL];
    NSString *htmlString = [[NSString alloc] initWithData:htmlData encoding:NSUTF8StringEncoding];

打印HTML

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="Content-Style-Type" content="text/css">
<title></title>
<meta name="Generator" content="Cocoa HTML Writer">
<style type="text/css">
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 16.0px '.SF UI Text'}
span.s1 {font-family: '.SFUIText'; font-weight: normal; font-style: normal; font-size: 16.00pt}
</style>
</head>
<body>
<p class="p1"><span class="s1">Test</span></p>
</body>
</html>

Extra space in reasulu

哪个错误应该只显示测试。我怎么能实现这个

1 个答案:

答案 0 :(得分:1)

-(NSMutableAttributedString*)getHtmlColorStringForCustomStyle:(NSString*)customString withBold:(NSString*)boldString italic:(NSString*)italicString andUnderLine:(NSString*)underlineString withColor:(NSString*)color

{

NSMutableAttributedString * attStringforCustomStyle = [[NSMutableAttributedString alloc] initWithData:[customString dataUsingEncoding:NSUTF8StringEncoding] options:@ {NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType

} documentAttributes:nil error:nil];

[attStringforCustomStyle beginEditing];

[attStringforCustomStyle addAttribute:NSForegroundColorAttributeName value:[UIColor colorwithHexString:color alpha:1.0] range:NSMakeRange(0,customString.length)];

if ([boldString isEqualToString:@"yes"])
{
    [attStringforCustomStyle addAttribute:NSFontAttributeName
                                    value:[UIFont boldSystemFontOfSize:12]
                                    range:NSMakeRange(0, attStringforCustomStyle.length)];
}

if ([italicString isEqualToString:@"yes"]) //Put in else if because it does not apply both in single string
{

    [attStringforCustomStyle addAttribute:NSFontAttributeName
                                    value:[UIFont italicSystemFontOfSize:12]
                                    range:NSMakeRange(0, attStringforCustomStyle.length)];
}

if ([underlineString isEqualToString:@"yes"])
{

    [attStringforCustomStyle addAttribute:NSUnderlineStyleAttributeName
                                    value:@(NSUnderlineStyleSingle)
                                    range:NSMakeRange(0, attStringforCustomStyle.length)];
}


[attStringforCustomStyle endEditing];

return attStringforCustomStyle;

}