iOS中支持HTML文本的标签控件——MDHTMLLabel

iOS中支持HTML文本的标签控件——MDHTMLLabel

1、引言

        在iOS开发中对HTML的处理不少时候除了使用WebView外,还须要原生的控件对其进行渲染,例如将HTML字符串渲染为图文混排的View视图。Git上有不少轻量级的HTML渲染框架,列举一些以下:html

RTLabel:基于UIView的HTML文本渲染控件,git地址:https://github.com/honcheng/RTLabelgit

RCLabel:与RTLabel思路相同,基于RCLabel之上,也是UIView的子类,支持了对HTML中的本地图片标签进行渲染。git地址:https://github.com/Janak-Nirmal/RichContentLabelgithub

MDHTMLLabel:与RTLabel和RCLabel不一样的是,其是UILabel的子类,更加轻量级,不能支持图片标签。git地址:https://github.com/mattdonnelly/MDHTMLLabelapp

    关于RCLabel对图片便签的支持,其只能支持本地的图片,不能支持远程URL图片连接,这在开发中将十分局限,之前我曾加RCLabel作了改造,加了支持远程图片URL的方法,我把它集成在了一个基础框架中,须要的伙伴能够参考下,git地址:https://github.com/ZYHshao/YHBaseFoundationTest。配套的讲解博客地址以下:http://my.oschina.net/u/2340880/blog/499311框架

    本篇博客主要讨论MDHTMLLabel的使用。atom

2、MDHTMLLabel的建立与设置

      MDHTMLLabel框架十分小巧,其中只有两个文件,总计2000余行代码。经过HTML字符串来建立一个MDHTMLLabel控件示例代码以下:spa

NSString * kDemoText = @"<a href='http://github.com/mattdonnelly/MDHTMLLabel'>MDHTMLLabel</a> is a lightweight, easy to use replacement for <b>UILabel</b> which allows you to fully <font face='Didot-Italic' size='19'>customize</font> the appearence of the text using HTML (with a few added features thanks to <b>CoreText</b>), as well letting you handle whenever a user taps or holds down on link and automatically detects ones not wrapped in anchor tags/>";
    MDHTMLLabel *htmlLabel = [[MDHTMLLabel alloc] initWithFrame:self.view.frame];
    htmlLabel.numberOfLines = 0;
    htmlLabel.htmlText = kDemoText;
    [self.view addSubview:htmlLabel];

效果以下图所示:.net

MDHTMLLabel中能够设置的一些属性解析以下:代理

//设置超连接文字的属性字典 和设置AttributeString方法一致
@property (nonatomic, strong) NSDictionary *linkAttributes;
//设置超连接文字激活时的属性字典
@property (nonatomic, strong) NSDictionary *activeLinkAttributes;
//设置超连接非激活时的属性字典
@property (nonatomic, strong) NSDictionary *inactiveLinkAttributes;
//设置超连接文字触发长按事件的最小按下时间
@property (nonatomic, assign) NSTimeInterval minimumPressDuration;
//设置label文件阴影的模糊半径
@property (nonatomic, assign) CGFloat shadowRadius;
//设置label在高亮状态下的文字模糊半径 注:非高亮状态的由原生UILabel的属性设置
@property (nonatomic, assign) CGFloat highlightedShadowRadius;
//设置label在高亮状态下的文字阴影偏移 注:非高亮状态的由原生UILabel的属性设置
@property (nonatomic, assign) CGSize highlightedShadowOffset;
//设置在label高亮状态下的文字阴影颜色 注:非高亮状态的由原生UILabel的属性设置
@property (nonatomic, strong) UIColor *highlightedShadowColor;
//设置首行文字的缩进距离
@property (nonatomic, assign) CGFloat firstLineIndent;
//设置文字的行间距
@property (nonatomic, assign) CGFloat leading;
//设置行高的倍数
@property (nonatomic, assign) CGFloat lineHeightMultiple;
//设置文字内容的边距
@property (nonatomic, assign) UIEdgeInsets textInsets;
//设置文字垂直方向的对其模式 默认为居中对其 MDHTMLLabelVerticalAlignment枚举意义以下:
/*
typedef NS_ENUM(NSUInteger, MDHTMLLabelVerticalAlignment) {
    MDHTMLLabelVerticalAlignmentCenter   = 0, //居中对其
    MDHTMLLabelVerticalAlignmentTop      = 1, //顶部对其
    MDHTMLLabelVerticalAlignmentBottom   = 2, //底部对其
};
*/
@property (nonatomic, assign) MDHTMLLabelVerticalAlignment verticalAlignment;
//设置文字的截断模式
@property (nonatomic, strong) NSString *truncationTokenString;
//根据内容获取控件尺寸
+ (CGFloat)sizeThatFitsHTMLString:(NSString *)htmlString
                         withFont:(UIFont *)font
                      constraints:(CGSize)size
           limitedToNumberOfLines:(NSUInteger)numberOfLines;

关于HTML数据中的超连接的相应,MDHTMLLabel是经过代理回调的方式处理的,以下:code

@protocol MDHTMLLabelDelegate <NSObject>
@optional
//点击超连接的时候触发的方法
- (void)HTMLLabel:(MDHTMLLabel *)label didSelectLinkWithURL:(NSURL*)URL;
//长按超连接时触发的方法
- (void)HTMLLabel:(MDHTMLLabel *)label didHoldLinkWithURL:(NSURL*)URL;
@end

专一技术,热爱生活,交流技术,也作朋友。

——珲少 QQ群:203317592

相关文章
相关标签/搜索