iOS 富文本加载和上传

  需要在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;

//下面是根据相应的富文本提取出相应的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];

//给富文本的相应字段添加相应的属性



这个'str'是带有HTML文本的富文本  上面是显示  给转化好的富文本添加固定的格式. 计算富文本的高度, 以及从编辑的富文本中提取出相应的HTML文本存一下以后不用百度!

你可能感兴趣的:(iOS 富文本加载和上传)