2020-1-13

iOS点击文本查看内容

- (void)viewDidLoad {

    [super viewDidLoad];

    UITextView* textView = [[UITextViewalloc]initWithFrame:CGRectMake(80,80,200,1000)];

    NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc]initWithString:@"我已经阅知了,请详细阅读内容,《健康告知》、《法律条文》、《必备条款》,。"];

    textView.backgroundColor= [UIColorredColor];

    textView.userInteractionEnabled = YES;

    textView.editable=NO;

    textView.scrollEnabled=NO;

    textView.contentInset = UIEdgeInsetsZero;

    textView.textContainerInset = UIEdgeInsetsZero;

    textView.textContainer.lineFragmentPadding = 0;

    for(UIGestureRecognizer*gesterintextView.gestureRecognizers) {

        gester.enabled=NO;

    }

    UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapClick:)];

    tap.numberOfTapsRequired = 1;

    tap.cancelsTouchesInView = NO;

    [textViewaddGestureRecognizer:tap];

    textView.attributedText= attrStr;

    textView.contentInset = UIEdgeInsetsZero;

     textView.textContainerInset = UIEdgeInsetsZero;

    textView.textContainer.lineFragmentPadding = 0;

    [textViewsizeToFit];

    [self.viewaddSubview:textView];

    [self.storage addLayoutManager:self.layoutManager];

}

-(void)tapClick:(UITapGestureRecognizer *)tap{

    if([tap.viewisMemberOfClass:[UITextViewclass]]) {

        UITextView* textView = (UITextView*)tap.view;

    NSArray * arry = @[@"《健康告知》",@"《法律条文》",@"《必备条款》"];

    CGPoint point = [taplocationInView:tap.view];

    NSInteger  index =  [textView.layoutManagerglyphIndexForPoint:pointinTextContainer:textView.textContainer];

CGRect  rect  = [textView.layoutManager boundingRectForGlyphRange:NSMakeRange(textView.attributedText.string.length-1, 1) inTextContainer:textView.textContainer];

        //点击的空白区域返回

        if(index == (textView.attributedText.string.length-1) && point.x> (rect.origin.x+rect.size.height) ){

            return;

        }

        for(inti =0; i< arry.count; i++) {

               NSRangerange = [textView.attributedText.stringrangeOfString:arry[i]];

               if(index >= range.location&& index < (range.location+range.length)) {

                   NSLog(@"---------index:%ld-------text:%@",(long)index,arry[i]);

               }

    }

    }

}

你可能感兴趣的:(2020-1-13)