最新国产好看的视频,伊人天堂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ì)的示例代碼,對大家具有一定的參考價(jià)值,需要的朋友們下面來一起看看吧。

前言

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

實(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];
}

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

總結(jié)

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

相關(guān)文章

最新評論

庄浪县| 英超| 长顺县| 金昌市| 富源县| 泉州市| 杭州市| 富平县| 太仆寺旗| 苍南县| 聂荣县| 连江县| 东乌| 治县。| 青岛市| 肥城市| 吉安县| 东丽区| 衡南县| 白山市| 保山市| 阿勒泰市| 舞钢市| 德保县| 平果县| 千阳县| 鸡泽县| 温宿县| 长海县| 澄城县| 连云港市| 奈曼旗| 仙游县| 拉孜县| 玉田县| 昆山市| 大庆市| 介休市| 孝义市| 宜阳县| 宣化县|