UILabel中生成链接并且打开链接地址
2016-06-16 10:26
489 查看
前段时间遇到一个问题需要把UILabel显示时遇到网址便自动转化成网络链接,最终效果如下
实现步骤如下:首先在https://github.com/petrpavlik/PPLabel下载PPLabel并导入到自己的工程文件,然后在需要使用的
#import "PPLabel.h"
添加这个协议:PPLabelDelegate
self.label=[[PPLabel
alloc]initWithFrame:CGRectMake(20,
40,
SCREEN_WIDTH-40, rect.size.height
+5)];
self.label.delegate=self;
self.label.text=[_FAG
integerValue]==1?_taskmodel.des:_taskupmodel.des;
self.label.font
= [UIFont
systemFontOfSize:15];
self.label.textColor =
HEXCOLOR(0X888888);
self.label.numberOfLines=0;
[view addSubview:self.label];
//self.label=desLabel;
// self.label.delegate=self;
NSError *error =
NULL;
NSDataDetector *detector = [NSDataDetector
dataDetectorWithTypes:NSTextCheckingTypeLink
error:&error];
self.matches = [detector
matchesInString:self.label.text
options:0
range:NSMakeRange(0,
self.label.text.length)];
[self
highlightLinksWithIndex:NSNotFound];
- (void)label:(PPLabel *)label didBeginTouch:(UITouch *)touch onCharacterAtIndex:(CFIndex)charIndex
{
[self
highlightLinksWithIndex:charIndex];
}
- (void)label:(PPLabel *)label didMoveTouch:(UITouch *)touch onCharacterAtIndex:(CFIndex)charIndex
{
[self
highlightLinksWithIndex:charIndex];
}
- (void)label:(PPLabel *)label didEndTouch:(UITouch *)touch onCharacterAtIndex:(CFIndex)charIndex
{
[self
highlightLinksWithIndex:NSNotFound];
for (NSTextCheckingResult *match
in self.matches) {
if ([match
resultType] == NSTextCheckingTypeLink) {
NSRange matchRange = [match
range];
if ([self
isIndex:charIndex
inRange:matchRange]) {
[[UIApplication
sharedApplication]
openURL:match.URL];
break;
}
}
}
}
- (void)label:(PPLabel *)label didCancelTouch:(UITouch *)touch {
[self
highlightLinksWithIndex:NSNotFound];
}
#pragma mark -
- (BOOL)isIndex:(CFIndex)index inRange:(NSRange)range {
return index > range.location && index < range.location+range.length;
}
- (void)highlightLinksWithIndex:(CFIndex)index {
NSMutableAttributedString* attributedString = [self.label.attributedText
mutableCopy];
for (NSTextCheckingResult *match
in self.matches) {
if ([match
resultType] == NSTextCheckingTypeLink) {
NSRange matchRange = [match
range];
if ([self
isIndex:index inRange:matchRange]) {
[attributedString addAttribute:NSForegroundColorAttributeName
value:[UIColor
grayColor] range:matchRange];
}
else {
[attributedString addAttribute:NSForegroundColorAttributeName
value:[UIColor
blueColor] range:matchRange];
}
[attributedString addAttribute:NSUnderlineStyleAttributeName
value:[NSNumber
numberWithInteger:NSUnderlineStyleSingle]
range:matchRange];
}
}
self.label.attributedText = attributedString;
}
实现步骤如下:首先在https://github.com/petrpavlik/PPLabel下载PPLabel并导入到自己的工程文件,然后在需要使用的
#import "PPLabel.h"
添加这个协议:PPLabelDelegate
self.label=[[PPLabel
alloc]initWithFrame:CGRectMake(20,
40,
SCREEN_WIDTH-40, rect.size.height
+5)];
self.label.delegate=self;
self.label.text=[_FAG
integerValue]==1?_taskmodel.des:_taskupmodel.des;
self.label.font
= [UIFont
systemFontOfSize:15];
self.label.textColor =
HEXCOLOR(0X888888);
self.label.numberOfLines=0;
[view addSubview:self.label];
//self.label=desLabel;
// self.label.delegate=self;
NSError *error =
NULL;
NSDataDetector *detector = [NSDataDetector
dataDetectorWithTypes:NSTextCheckingTypeLink
error:&error];
self.matches = [detector
matchesInString:self.label.text
options:0
range:NSMakeRange(0,
self.label.text.length)];
[self
highlightLinksWithIndex:NSNotFound];
- (void)label:(PPLabel *)label didBeginTouch:(UITouch *)touch onCharacterAtIndex:(CFIndex)charIndex
{
[self
highlightLinksWithIndex:charIndex];
}
- (void)label:(PPLabel *)label didMoveTouch:(UITouch *)touch onCharacterAtIndex:(CFIndex)charIndex
{
[self
highlightLinksWithIndex:charIndex];
}
- (void)label:(PPLabel *)label didEndTouch:(UITouch *)touch onCharacterAtIndex:(CFIndex)charIndex
{
[self
highlightLinksWithIndex:NSNotFound];
for (NSTextCheckingResult *match
in self.matches) {
if ([match
resultType] == NSTextCheckingTypeLink) {
NSRange matchRange = [match
range];
if ([self
isIndex:charIndex
inRange:matchRange]) {
[[UIApplication
sharedApplication]
openURL:match.URL];
break;
}
}
}
}
- (void)label:(PPLabel *)label didCancelTouch:(UITouch *)touch {
[self
highlightLinksWithIndex:NSNotFound];
}
#pragma mark -
- (BOOL)isIndex:(CFIndex)index inRange:(NSRange)range {
return index > range.location && index < range.location+range.length;
}
- (void)highlightLinksWithIndex:(CFIndex)index {
NSMutableAttributedString* attributedString = [self.label.attributedText
mutableCopy];
for (NSTextCheckingResult *match
in self.matches) {
if ([match
resultType] == NSTextCheckingTypeLink) {
NSRange matchRange = [match
range];
if ([self
isIndex:index inRange:matchRange]) {
[attributedString addAttribute:NSForegroundColorAttributeName
value:[UIColor
grayColor] range:matchRange];
}
else {
[attributedString addAttribute:NSForegroundColorAttributeName
value:[UIColor
blueColor] range:matchRange];
}
[attributedString addAttribute:NSUnderlineStyleAttributeName
value:[NSNumber
numberWithInteger:NSUnderlineStyleSingle]
range:matchRange];
}
}
self.label.attributedText = attributedString;
}
相关文章推荐
- Android中使用系统自带浏览器打开本地assets目录下的html文件
- Android使用webview调用系统浏览器以及项目内显示并支持图片和输入
- jQuery新窗口打开外链接
- jQuery新窗口打开外链接
- 解决错误“Dynamic Web Module 3.0 requires Java 1.6 or newer.”
- nested exception is com.mysql.jdbc.PacketTooBigException: Packet for query is too large (1109 > 1024
- 修改UITextField的placeholder颜色
- iOS9之UIStackView体验,无需任何约束,这才是真正的自动布局,快到不能呼吸
- string stringBuffer stringBuilder性能比较
- iOS 主线程更新UI
- <Sicily> Longest Common Subsequence
- Android Studio编译程序出现finished with non-zero exit value 2错误
- AsyncTask和Handler和runOnUiThread()
- UISegmentedControl控制滚动试图
- UITableView常用属性和方法
- java中break和continue的区别详解(附源码)
- iOS中的表单按钮选项UIActionSheet常用方法整理
- JS window.requestAnimFrameS 是什么意思
- jira+Confluence+FishEye安装破解集成
- 【OBJC类扩展之尺寸】UIView+Size