创作说明:东西是跟别人看的,一定要"高内聚低耦合",清楚易懂
#版权声明:
来源:https://github.com/cxa/CXAHyperlinkLabel
作者: CHEN Xian-an
介绍:给 UILabel 中的文字加上超链接。并支持两种点击方式
一种是单击(single click),一种是长按(long press)。
开始编写
#应广大网友支持的欢呼声。百度云链接已经找回。但是demo没有了。按照这个方法集成是可以的。
1.先去我的百度网盘:http://pan.baidu.com/s/1qY6rw1e下载控件封装包,提取码是:8k9x
,下载完成后拖入到你的项目中
2.配置工程
-1.添加支持库
2.配置工程
输入的文字是:$(SDK_ROOT)/usr/include/libxml2
3.代码编写
只需要在文件中写两个方法即可
- (void)loadView
{
//1.创建视图UIScrollView,防止文字过长,超出屏幕
//具体情况具体分析,有时可以不添加UIScrollView
UIScrollView *sv = [[UIScrollView alloc] initWithFrame:[UIScreen mainScreen].applicationFrame];
sv.contentSize = sv.bounds.size;
self.view = sv;
self.view.backgroundColor = [UIColor colorWithHue:0 saturation:0 brightness:.9 alpha:1];
//2。文本的赋值以及设置
NSArray *URLs;
NSArray *URLRanges;
NSAttributedString *as = [NSAttributedString attributedString:&URLs URLRanges:&URLRanges testContent:self.schoolModel.Content];
_label = [[CXAHyperlinkLabel alloc] initWithFrame:CGRectZero];
_label.numberOfLines = 0;
_label.backgroundColor = [UIColor clearColor];
_label.attributedText = as;
_label.font = [UIFont systemFontOfSize:13];
[_label setURLs:URLs forRanges:URLRanges];
//3.点击超链接后的跳转事件
__weak typeof(self) unsafeSelf = self;
_label.URLClickHandler = ^(CXAHyperlinkLabel *label, NSURL *URL, NSRange range, NSArray *textRects){
//再次编写点击跳转事件:
//比如见下面:
WebViewController *webView = [[WebViewController alloc] initWithTitle:@"网页主页" withUrl:[URL absoluteString]];
[unsafeSelf.navigationController pushViewController:webView animated:YES];
};
//4.长按事件的回调方法
_label.URLLongPressHandler = ^(CXAHyperlinkLabel *label, NSURL *URL, NSRange range, NSArray *textRects){
//再次编写回调的方法
//再次编写回调的方法
};
//5.添加label
[self.view addSubview:_label];
}
#pragma mark - System
//布局
- (void)viewWillLayoutSubviews
{
CGFloat margin = 10.;
CGSize size = CGRectInset(self.view.bounds, margin, margin).size;
size.height = INT16_MAX;
CGSize labelSize = [_label sizeThatFits:size];
labelSize.width = size.width;
_label.frame = (CGRect){CGPointMake(margin, margin), labelSize};
CGFloat height = CGRectGetMaxY(_label.frame) + margin;
if (height < CGRectGetHeight(self.view.bounds))
height = CGRectGetHeight(self.view.bounds);
((UIScrollView *)self.view).contentSize = (CGSize){CGRectGetWidth(self.view.bounds), height};
}
5.效果图