Android中TextView自動(dòng)識(shí)別url且實(shí)現(xiàn)點(diǎn)擊跳轉(zhuǎn)
前言
在項(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ì)腳本之家的支持。
- Android利用CountDownTimer實(shí)現(xiàn)點(diǎn)擊獲取驗(yàn)證碼倒計(jì)時(shí)效果
- Android實(shí)現(xiàn)點(diǎn)擊獲取驗(yàn)證碼倒計(jì)時(shí)效果
- Android賬號(hào)注冊(cè)實(shí)現(xiàn)點(diǎn)擊獲取驗(yàn)證碼倒計(jì)時(shí)效果
- Android中TextView實(shí)現(xiàn)部分文字可點(diǎn)擊跳轉(zhuǎn)
- Android studio點(diǎn)擊跳轉(zhuǎn)WebView詳解
- Android TextView中文本點(diǎn)擊文字跳轉(zhuǎn) (代碼簡(jiǎn)單)
- Android開發(fā)歡迎頁(yè)點(diǎn)擊跳過(guò)倒計(jì)時(shí)進(jìn)入主頁(yè)
相關(guān)文章
Android通訊錄開發(fā)之刪除功能的實(shí)現(xiàn)方法
這篇文章主要介紹了Android通訊錄開發(fā)之刪除功能的實(shí)現(xiàn)方法,有需要的朋友可以參考一下2014-01-01
XListView實(shí)現(xiàn)網(wǎng)絡(luò)加載圖片和下拉刷新
這篇文章主要為大家詳細(xì)介紹了XListView實(shí)現(xiàn)網(wǎng)絡(luò)加載圖片和下拉刷新,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-11-11
常見的8個(gè)Android內(nèi)存泄漏問(wèn)題及解決方法
在Android開發(fā)中,內(nèi)存泄漏是一個(gè)常見的問(wèn)題,這個(gè)問(wèn)題可能會(huì)導(dǎo)致應(yīng)用程序變慢、崩潰或者消耗大量的內(nèi)存,最終導(dǎo)致設(shè)備性能下降,本文就給大家總結(jié)一下最常見的8個(gè)Android內(nèi)存泄漏問(wèn)題及解決方法,需要的朋友可以參考下2023-07-07
Android實(shí)現(xiàn)滑動(dòng)刻度尺效果
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)滑動(dòng)刻度尺效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-06-06
Android編程使用ListView實(shí)現(xiàn)數(shù)據(jù)列表顯示的方法
這篇文章主要介紹了Android編程使用ListView實(shí)現(xiàn)數(shù)據(jù)列表顯示的方法,實(shí)例分析了Android中ListView控件的使用技巧,需要的朋友可以參考下2016-01-01
Android ActionBar完全解析使用官方推薦的最佳導(dǎo)航欄(下)
這篇文章主要介紹了Android ActionBar完全解析使用官方推薦的最佳導(dǎo)航欄(下) ,需要的朋友可以參考下2017-04-04
Android組件TabHost實(shí)現(xiàn)頁(yè)面中多個(gè)選項(xiàng)卡切換效果
這篇文章主要為大家詳細(xì)介紹了Android組件TabHost實(shí)現(xiàn)頁(yè)面中多個(gè)選項(xiàng)卡切換效果的相關(guān)資料,感興趣的小伙伴們可以參考一下2016-05-05
Android中毛玻璃效果的兩種實(shí)現(xiàn)代碼
這篇文章主要介紹了Android中毛玻璃效果的兩種實(shí)現(xiàn)代碼,第一種是使用JAVA算法FastBlur實(shí)現(xiàn),第二種是使用Android自帶類RenderScript 實(shí)現(xiàn),本文通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友參考下吧2024-08-08

