? 需要在UIlable 和UItextView 上加載帶有HTML的富文本
NSData *data = [str dataUsingEncoding:NSUnicodeStringEncoding]; ? ? ?
NSDictionary *options = @{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType, NSFontAttributeName:KTextFount15};
NSMutableAttributedString *htmlInfo = [[NSMutableAttributedString alloc]initWithData:data
options:options
documentAttributes:nil
error:nil];
NSDictionary *dicc = @{NSFontAttributeName:KTextFount14, NSForegroundColorAttributeName:[UIColor colorWithHexString:@"#545454"]};
[htmlInfo addAttributes:dicc range:NSMakeRange(0, htmlInfo.length)];
CGFloat height = [htmlInfo boundingRectWithSize:CGSizeMake(SCREEN_WIDTH - 20, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin context:nil].size.height +15;
self.contentLab.attributedText = htmlInfo;
//下面是根據(jù)相應(yīng)的富文本提取出相應(yīng)的HTML文本
NSDictionary *dic = @{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute :[NSNumber numberWithInt:NSUTF8StringEncoding]};
NSData *htmlData = [attribute dataFromRange:NSMakeRange(0, attribute.length) documentAttributes:dic error:nil];
htmlStr = [[NSString alloc] initWithData:htmlData encoding:NSUTF8StringEncoding];
//給富文本的相應(yīng)字段添加相應(yīng)的屬性
這個'str'是帶有HTML文本的富文本 ?上面是顯示 ?給轉(zhuǎn)化好的富文本添加固定的格式. 計算富文本的高度, 以及從編輯的富文本中提取出相應(yīng)的HTML文本存一下以后不用百度!