android富文本转换为html标签,textView富文本AttributedString,转换成html

WebView虽然也可以实现图文混排的效果,但是在使用的时候比较复杂,而且WebVIew的内存泄漏相当明显,使用不好反而得补偿失。现在很多的新闻类App都采用自己写的富文本进行图文混排。

下载已经封装好的富文本标签类RCLabel(http://www.okbase.net/file/item/20694)

该类是由一牛人封装好的用于实现图文混排的富文本类,在下载下来的源代码中可以看到其用法,但是这里有一个较为复杂的问题是你每次都需要手动将你要显示的内容封装成HTML格式的文本,这个过程相当繁琐而且容易出错,所以我在这个基础上进行了改进,将封装HTML的方法通过正则表达式独立开来,这样就实现了RCLabel与正则实现图文混排。

下面是正文textView:

富文本基本使用方法的思路概要:

1、创建一个NSMutableAttributedString富文本对象(一般不用NSAttributedString)

2、设置addAttribute属性

(1) addAttribute: 一个属性

(2) addAttributes: 一个存储多个属性的属性字典,比如这个字典可以是:

NSDictionary *attrDic = @{

NSFontAttributeName: [UIFont fontWithName: @"Zapfino" size: 15],

NSForegroundColorAttributeName: [UIColor blueColor]

};

3、控件.attributedText = 富文本对象(和控件.txt = NSString文本对象不一样的)

富文本内容的传输,在iOS中其实就是attributeText的表现吧。如后台发了一段html内容过来,经过处理变成attributeText,然后交由textView呈现:

/** 将超文本格式化为富文本 */

(NSAttributedString *)htmlAttributeStringByHtmlString:(NSString *)htmlString{

NSAttributeString *attributeString;

NSData *htmlData = [htmlString dataUsingEncoding:NSUTF8StringEncoding];

NSDictionary importParams = @{NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType,NSCharacterEncodingDocumentAttribute:[NSNumber numberWithInt:NSUTF8StringEncoding];

}

NSError error = nil;

attributeString = [NSAttributedString alloc] initWithData:htmlData options:importParams documentAttributes:NULL error:&error];

return attributeString;

}

同理,textView编辑富文本同是attributeText,那当然可以转为html,再发送给后天了。

/ 将富文本格式化为超文本 */

(NSString *)htmlStringByHtmlAttributeString:(NSAttributedString *)htmlAttributeString{

NSString *htmlString;

NSDictionary *exportParams = @{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,NSCharacterEncodingDocumentAttribute:[NSNumber numberWithInt:NSUTF8StringEncoding]

};

NSData *htmlData = [htmlAttributeString dataFormRange:NSMakeRange(0, htmlAttributeString.length) documentAttributes:exporParams error:nil];

htmlString = [NSString alloc] initWithData:htmlData encoding: NSUTF8StringEncoding];

return htmlString;

}

利用以上两个方法,就可以完成接收html的格式转换显示与html的格式转换后post给后台了。

AttributedString究竟可以设置哪些属性,具体来说,有以下21个:

NSFontAttributeName 设置字体属性,默认值:字体:Helvetica(Neue) 字号:12

NSForegroundColorAttributeNam 设置字体颜色,取值为 UIColor对象,默认值为黑色

NSBackgroundColorAttributeName 设置字体所在区域背景颜色,取值为 UIColor对象,默认值为nil, 透明色

NSLigatureAttributeName 设置连体属性,取值为NSNumber 对象(整数),0 表示没有连体字符,1 表示使用默认的连体字符

NSKernAttributeName 设定字符间距,取值为 NSNumber 对象(整数),正值间距加宽,负值间距变窄

NSStrikethroughStyleAttributeName 设置删除线,取值为 NSNumber 对象(整数)

NSStrikethroughColorAttributeName 设置删除线颜色,取值为 UIColor 对象,默认值为黑色

NSUnderlineStyleAttributeName 设置下划线,取值为 NSNumber 对象(整数),枚举常量 NSUnderlineStyle中的值,与删除线类似

NSUnderlineColorAttributeName 设置下划线颜色,取值为 UIColor 对象,默认值为黑色

NSStrokeWidthAttributeName 设置笔画宽度,取值为 NSNumber 对象(整数),负值填充效果,正值中空效果

NSStrokeColorAttributeName 填充部分颜色,不是字体颜色,取值为 UIColor 对象

NSShadowAttributeName 设置阴影属性,取值为 NSShadow 对象

NSTextEffectAttributeName 设置文本特殊效果,取值为 NSString 对象,目前只有图版印刷效果可用:

NSBaselineOffsetAttributeName 设置基线偏移值,取值为 NSNumber (float),正值上偏,负值下偏

NSObliquenessAttributeName 设置字形倾斜度,取值为 NSNumber (float),正值右倾,负值左倾

NSExpansionAttributeName 设置文本横向拉伸属性,取值为 NSNumber (float),正值横向拉伸文本,负值横向压缩文本

NSWritingDirectionAttributeName 设置文字书写方向,从左向右书写或者从右向左书写

NSVerticalGlyphFormAttributeName 设置文字排版方向,取值为 NSNumber 对象(整数),0 表示横排文本,1 表示竖排文本

NSLinkAttributeName 设置链接属性,点击后调用浏览器打开指定URL地址

NSAttachmentAttributeName 设置文本附件,取值为NSTextAttachment对象,常用于文字图片混排

NSParagraphStyleAttributeName 设置文本段落排版格式,取值为 NSParagraphStyle 对象

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值