最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android中TextView自動(dòng)識(shí)別url且實(shí)現(xiàn)點(diǎn)擊跳轉(zhuǎn)

 更新時(shí)間:2017年03月23日 08:23:51   作者:cj小牛  
這篇文章主要介紹了關(guān)于Android中TextView自動(dòng)識(shí)別url且實(shí)現(xiàn)點(diǎn)擊跳轉(zhuǎn)的相關(guān)資料,文中給出了詳細(xì)的示例代碼,對(duì)大家具有一定的參考價(jià)值,需要的朋友們下面來(lái)一起看看吧。

前言

在項(xiàng)目中要在展示展示的文字中,自動(dòng)匹配url 并且點(diǎn)擊實(shí)現(xiàn)跳轉(zhuǎn),看了很多第三方的感覺都很復(fù)雜。后來(lái)自己寫了一個(gè)簡(jiǎn)單的。

實(shí)現(xiàn)代碼如下

1、創(chuàng)建一個(gè)繼承UITextView的CjTextView 。

import <UIKit/UIKit.h>

@interface CjTextView : UITextView
@end

import "CjTextView.h"

@interface CjTextView ()
@property (nonatomic, copy ) NSString myText;
@property (nonatomic, assign) NSRange urlTange;
@property (nonatomic, copy ) NSString url;
@end

@implementation CjTextView
-(instancetype)initWithFrame:(CGRect)frame{
if (self = [super initWithFrame:frame]) {
}
return self;
}
// 重寫了text的set 方法
-(void)setText:(NSString * )text{
self.myText = text;
[self.textStorage setAttributedString:[[NSAttributedString alloc]initWithString:text]];
NSRange range = NSMakeRange(0, self.myText.length);
[self.textStorage addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:20] range:range];
在這個(gè)方法中可以改變網(wǎng)址的顏色字體大小等屬性。
[self doing];

}
-(void)layoutSubviews{
[super layoutSubviews];
self.textContainer.size = self.bounds.size;
}

-(void)touchesBegan:(NSSet<UITouch *> )touches withEvent:(UIEvent )event{
CGPoint point = [[touches anyObject] locationInView:self];
NSRange range =self.urlTange;
self.selectedRange = range;
NSArray array = [self selectionRectsForRange:self.selectedTextRange];
for (UITextSelectionRect obj in array) {
if (CGRectContainsPoint(obj.rect, point)) {
NSLog(@"你點(diǎn)擊了網(wǎng)址%@",_url);
}
}
}

-(void) doing {
NSDataDetector * dataDetector = [NSDataDetector dataDetectorWithTypes:NSTextCheckingAllTypes error:nil];
NSArray res= [dataDetector matchesInString:self.textStorage.string options:NSMatchingReportProgress range:NSMakeRange(0, self.textStorage.string.length)];
for (NSTextCheckingResultresult in res) {
self.urlTange = result.range;
NSString str = [self.textStorage.string substringWithRange:result.range];
self.url = str;
NSMutableAttributedString *att= [[NSMutableAttributedString alloc]initWithString:str];
[att addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(0, str.length)];
[att addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:20] range:NSMakeRange(0, str.length)];
[self.textStorage replaceCharactersInRange:result.range withAttributedString:att];
}
}
@end

2、在控制器中實(shí)現(xiàn)

include "CjTextView.h"

@interface ViewController ()
@end
@implementation ViewController

(void)viewDidLoad {
[super viewDidLoad];
CjTextView *label = [[CjTextView alloc]init];

關(guān)掉彈出鍵盤

label.editable = NO;
label.text = @"123發(fā)送的股份大概放到放到地方多福多壽http://baidu.comuiiyiroiqiotioq" ;
label.backgroundColor = [UIColor yellowColor];
label.frame = CGRectMake(100, 100, 202, 200);;
[self.view addSubview: label];
}

就是這么簡(jiǎn)單的代碼就實(shí)現(xiàn)了

總結(jié)

以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)各位Android開發(fā)者們能帶來(lái)一定的幫助,如果有疑問(wèn)大家可以留言交流,謝謝大家對(duì)腳本之家的支持。

相關(guān)文章

最新評(píng)論

英山县| 德化县| 东阿县| 罗江县| 永福县| 盐源县| 金门县| 甘孜县| 法库县| 济宁市| 仁化县| 巴马| 河源市| 上高县| 遂川县| 温宿县| 芷江| 淮安市| 海伦市| 平邑县| 邻水| 射洪县| 平南县| 柳江县| 延川县| 兴城市| 阿坝| 巴彦淖尔市| 准格尔旗| 包头市| 高邑县| 洱源县| 镇平县| 全州县| 陈巴尔虎旗| 杨浦区| 镇原县| 桃园县| 景宁| 利津县| 西吉县|