iOS點擊文本查看內(nèi)容
- (void)viewDidLoad {
? ? [super viewDidLoad];
? ? UITextView* textView = [[UITextViewalloc]initWithFrame:CGRectMake(80,80,200,1000)];
? ? NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc]initWithString:@"我已經(jīng)閱知了,請詳細閱讀內(nèi)容寞埠,《健康告知》屁置、《法律條文》、《必備條款》仁连,蓝角。"];
? ? 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];
? ? ? ? //點擊的空白區(qū)域返回
? ? ? ? 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]);
?? ? ? ? ? ? ? }
? ? }
? ? }
}