前言
創(chuàng)新互聯(lián)專業(yè)為企業(yè)提供延邊朝鮮族網(wǎng)站建設、延邊朝鮮族做網(wǎng)站、延邊朝鮮族網(wǎng)站設計、延邊朝鮮族網(wǎng)站制作等企業(yè)網(wǎng)站建設、網(wǎng)頁設計與制作、延邊朝鮮族企業(yè)網(wǎng)站模板建站服務,十載延邊朝鮮族做網(wǎng)站經(jīng)驗,不只是建網(wǎng)站,更提供有價值的思路和整體網(wǎng)絡服務。
在項目中要在展示展示的文字中,自動匹配url 并且點擊實現(xiàn)跳轉,看了很多第三方的感覺都很復雜。后來自己寫了一個簡單的。
實現(xiàn)代碼如下
1、創(chuàng)建一個繼承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]; 在這個方法中可以改變網(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)了
總結
以上就是這篇文章的全部內容了,希望本文的內容對各位Android開發(fā)者們能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對創(chuàng)新互聯(lián)的支持。
文章標題:Android中TextView自動識別url且實現(xiàn)點擊跳轉
URL網(wǎng)址:http://www.ekvhdxd.cn/article8/jecdop.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站改版、Google、營銷型網(wǎng)站建設、做網(wǎng)站、定制開發(fā)、網(wǎng)站營銷
聲明:本網(wǎng)站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)