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文本存一下以后不用百度!

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容