rtf 转html出现乱码

\rtf1\ansi\ansicpg936\cocoartf1404\cocoasubrtf470 {\fonttbl\f0\fswiss\fcharset0 Helvetica;} {\colortbl;\red255\green255\blue255;} \paperw11900\paperh16840\margl1440\margr1440\vieww10800\viewh8400\viewkind0 \pard\tx566\tx1133\tx1700\tx2267\tx2834\tx3401\tx3968\tx4535\tx5102\tx5669\tx6236\tx6803\pardirnatural\partightenfactor0 \f0\fs24 \cf0

应该提取要输入rtf文件的nsstring,然后用下面方法转化为unicode码,去除获取字符串里的<>号,然后加上\\‘ 格式,再组合成一个nsstring返回过来,然后直接插入到rtf中就可以了~ nsstring中不要有\特殊字符。

-(NSString *)getNSStringUNicode:(NSString *)ss{

NSMutableString *result=[[[NSMutableString alloc]init]autorelease];

for (int i=0; i

NSStringEncoding b=CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000);

NSData *data=[[ss substringWithRange:NSMakeRange(i, 1)] dataUsingEncoding:b];

NSString *middlestring=[[NSString alloc]initWithFormat:@"%@",data];

middlestring=[middlestring substringWithRange:NSMakeRange(1, middlestring.length-2)];

if(middlestring.length==4){

[result appendFormat:@"\\'%@\\'%@",[middlestring substringWithRange:NSMakeRange(0, 2)],[middlestring substringWithRange:NSMakeRange(2, 2)]];

}else{

[result appendFormat:@"\\'%@",middlestring];

}

}

return result;

}

你可能感兴趣的:(rtf 转html出现乱码)