UITextView笔记

来源:互联网 发布:全国数学建模大赛知乎 编辑:程序博客网 时间:2024/06/15 05:01

自定义UITextView,点击部分文字做事情.


//1.测试

#import "ViewController.h"

#import "MyTextView.h"


@interfaceViewController ()


@end


@implementation ViewController


- (void)viewDidLoad {

    [superviewDidLoad];

    

    MyTextView *textView = [[MyTextViewalloc] init];

    NSString *string =@"北方有佳人。绝世而独立。一顾倾人城。再顾倾人国。宁不知倾城与倾国。佳人难再得。";

    NSString *subString =@"佳人难再得";

    NSMutableAttributedString *attriString = [[NSMutableAttributedStringalloc] initWithString:string];

    [attriString setAttributes:@{NSForegroundColorAttributeName:[UIColorblueColor]}

                        range:NSMakeRange(0, string.length - subString.length)];

    

    NSRange range = [stringrangeOfString:subString];

    textView.specialRange = range;

    [attriString setAttributes:@{NSForegroundColorAttributeName:[UIColorredColor],

                                NSUnderlineStyleAttributeName:@(NSUnderlineStyleSingle)}

                        range:range];;

    

    //统一设置字体大小,否则字体不正确

    [attriString addAttribute:NSFontAttributeNamevalue:[UIFontsystemFontOfSize:16]range:NSMakeRange(0, string.length)];

    CGFloat w =200;

    CGSize size = [stringboundingRectWithSize:CGSizeMake(w,MAXFLOAT) options:NSStringDrawingUsesLineFragmentOriginattributes:@{NSFontAttributeName:[UIFontsystemFontOfSize:16]}context:nil].size;

    textView.frame =CGRectMake(60,100, size.width, size.height);

    

    //去除textView四周的间距,保证计算的frame正确

    textView.backgroundColor = [UIColorlightGrayColor];

    textView.attributedText = attriString;

    

    textView.block = ^(){

        NSLog(@"点了特殊文字");

    };

    [self.viewaddSubview:textView];

}


//2.自定义

#import <UIKit/UIKit.h>


typedefvoid(^specialBlock)();


@interface MyTextView :UITextView


/** 特殊文字的范围 */

@property (nonatomic,assign) NSRange specialRange;


@property (nonatomic,copy) specialBlock block;

@end

#import "MyTextView.h"


@implementation MyTextView


- (instancetype)initWithFrame:(CGRect)frame

{

    if (self = [superinitWithFrame:frame]) {

        self.backgroundColor = [UIColorclearColor];

        self.editable =NO;

        self.scrollEnabled=NO;

        self.textContainerInset =UIEdgeInsetsMake(0, -5,0, -5);

    }

    returnself;

}


- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event

{

    UITouch *touch = [touchesanyObject];

    CGPoint point = [touchlocationInView:self];

    self.selectedRange =NSMakeRange(0,0);

    if ([selftouchingPointInPoint:point]) {

        if (self.block) {

            self.block();

        }

    }

}


- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event

{

    return [selftouchingPointInPoint:point];

}


/**

 *  触摸点是否处理(除特殊文字以外的都不处理)

 */

- (BOOL)touchingPointInPoint:(CGPoint)point

{

    self.selectedRange =self.specialRange;

    

    //选中范围的矩形框数组(换行就会有多个rect

    NSArray *rects = [selfselectionRectsForRange:self.selectedTextRange];

    

    //清空选中范围(复制粘贴的范围)

    self.selectedRange =NSMakeRange(0,0);

    

    

    for (UITextSelectionRect *selectedRectin rects) {

        CGRect rect = selectedRect.rect;

        if (CGRectContainsPoint(rect, point)) {

            returnYES;

        }

    }

    returnNO;

}


1 0
原创粉丝点击