国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > Android > 正文

Android中TextView自動識別url且實現(xiàn)點擊跳轉(zhuǎn)

2019-12-12 03:18:36
字體:
供稿:網(wǎng)友

前言

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

實現(xiàn)代碼如下

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

import <UIKit/UIKit.h>@interface CjTextView : UITextView@endimport "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];在這個方法中可以改變網(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(@"你點擊了網(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、在控制器中實現(xiàn)

include "CjTextView.h"@interface ViewController ()@end@implementation ViewController(void)viewDidLoad {[super viewDidLoad];CjTextView *label = [[CjTextView alloc]init];

關掉彈出鍵盤

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

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

總結(jié)

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

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 板桥市| 红原县| 岗巴县| 磴口县| 冀州市| 杭锦后旗| 常熟市| 汝城县| 锦屏县| 仲巴县| 平定县| 东莞市| 安达市| 永修县| 德清县| 于田县| 新巴尔虎左旗| 常德市| 镇雄县| 盘山县| 武宣县| 五台县| 榕江县| 张掖市| 玉龙| 北京市| 绍兴市| 昭苏县| 永寿县| 青海省| 公安县| 包头市| 图木舒克市| 长宁区| 阿克陶县| 普格县| 左贡县| 香格里拉县| 来安县| 四子王旗| 四子王旗|