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];
在这个方法中可以改变网址的颜色字体大小等属性。
[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(@"你点击了网址%@",_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
include "CjTextView.h"
@interface ViewController ()
@end
@implementation ViewController
(void)viewDidLoad {
[super viewDidLoad];
CjTextView *label = [[CjTextView alloc]init];
label.editable = NO; label.text = @"123发送的股份大概放到放到地方多福多寿http://baidu.comuiiyiroiqiotioq" ; label.backgroundColor = [UIColor yellowColor]; label.frame = CGRectMake(100, 100, 202, 200);; [self.view addSubview: label]; }
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有