iOS7 textView处理URL链接 以及点击 长按手势的处理

举例1 :文本超链接

@property (weak, nonatomic) IBOutlet UITextView *tv;


    NSMutableAttributedString *attributedString = [[NSMutableAttributedString allocinitWithString:@"This is an example by @marcelofabri_"];

    [attributedString addAttribute:NSLinkAttributeName

                             value:@"username://marcelofabri_"

                             range:[[attributedString stringrangeOfString:@"@marcelofabri_"]];

    NSDictionary *linkAttributes = @{NSForegroundColorAttributeName: [UIColor greenColor],

                                     NSUnderlineColorAttributeName: [UIColor lightGrayColor],

                                     NSUnderlineStyleAttributeName@(NSUnderlinePatternSolid)};

    self.tv.linkTextAttributes = linkAttributes;

    self.tv.attributedText = attributedString;

    self.tv.delegate =self ;

    self.tv.editable = NO;


- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange{

    NSLog(@"%@",URL.absoluteString);

    return NO ;

}

举例2 :文本:系统默认是双击 ,这里用单击实现

    self.tv.dataDetectorTypes = UIDataDetectorTypeAll;

    self.tv.attributedText = [[NSAttributedString alloc]initWithString:

                               @" +8602980000000.\r\n"

                               "My personal web site www.xxxxxx.com.\r\n"

                               "My E-mail address is XXXXX@gmail.com.\r\n"

                               "I was born in 1900-01-01."];

   self.tv.editable = NO;


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

    [self.tv addGestureRecognizer:tap];




- (void)onlyTap:(UITapGestureRecognizer *)recognizer

{

    CGPoint location = [recognizer locationInView:self.tv];

    NSLog(@"Tap Gesture Coordinates: %.2f %.2f", location.x, location.y);

    NSString *tappedSentence = [self lineAtPosition:CGPointMake(location.x, location.y)];

    NSLog(@"%@",tappedSentence);


}


- (NSString *)lineAtPosition:(CGPoint)position

{

    //eliminate scroll offset

    position.y += _tv.contentOffset.y;

    //get location in text from textposition at point

    UITextPosition *tapPosition = [_tv closestPositionToPoint:position];

    //fetch the word at this position (or nil, if not available)

    UITextRange *textRange = [_tv.tokenizer rangeEnclosingPosition:tapPosition withGranularity:UITextGranularitySentence inDirection:UITextLayoutDirectionRight];

    return [_tv textInRange:textRange];

}




参考资料:

http://stackoverflow.com/questions/15034652/tap-gesture-to-part-of-a-uitextview

http://www.raywenderlich.com/48001/easily-overlooked-new-features-ios-7#textViewLinks







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值