zoukankan      html  css  js  c++  java
  • UITextView 实现链接点击事件

    UILabel通过富文本可以实现图文混排,但是想要实现文字的点击效果好像不容易实现,这里有2种方法可以达到效果

    1. YYLabel -->YYText框架
      参考我之前的博客:http://www.cnblogs.com/qqcc1388/p/6709336.html

    2. UITextView 系统原生的UI框架实现文字点击效果

    #import "ViewController.h"
    
    @interface ViewController ()<UITextViewDelegate>
    
    @property (nonatomic,strong) UITextView *textView;
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
        
        UITextView *textView  = [[UITextView alloc] initWithFrame:CGRectMake(20, 100, self.view.bounds.size.width - 20, 44)];
        textView.delegate = self;
        textView.editable = NO;
        textView.scrollEnabled = NO;
        self.textView = textView;
        NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] initWithString:@"客服电话:400-775-5333 转4 (每天9:00-20:00)"];
        [attr setAttributes:@{NSForegroundColorAttributeName:[UIColor blackColor],NSFontAttributeName:[UIFont systemFontOfSize:14]} range:NSMakeRange(0, attr.length)];
        //点击了链接拨打电话
        [attr setAttributes:@{NSForegroundColorAttributeName:[UIColor colorWithRed:0 green:147/255.0 blue:238/255.0 alpha:1],
                              NSFontAttributeName:[UIFont systemFontOfSize:14],
                              NSLinkAttributeName:@"telprompt://400-775-5333"}
         
         //如果点击了之后跳转网页可以将NSLinkAttributeName后面的修改为想要跳转的网页
                      range:NSMakeRange(5, 15)];
        textView.attributedText = attr;
        [self.view addSubview:textView];
        
    }
    
    #pragma mark - 点击了链接会走这个方法 可以在这里处理操作
    -(BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange{
        return YES;
    }
    
    
    @end
    

    方法2效果是可以达到,但是由于textView本身点击后会有UIMenuController弹出效果,比较影响体验,找了很多方法也没有找到很好的解决方案,加上实现上面偏复杂,掌握实现原理即可,推荐方法1来实现类似的效果。

  • 相关阅读:
    iSCSI又称为IPSAN
    文档类型定义DTD
    HDU 2971 Tower
    HDU 1588 Gauss Fibonacci
    URAL 1005 Stone Pile
    URAL 1003 Parity
    URAL 1002 Phone Numbers
    URAL 1007 Code Words
    HDU 3306 Another kind of Fibonacci
    FZU 1683 纪念SlingShot
  • 原文地址:https://www.cnblogs.com/qqcc1388/p/6709648.html
Copyright © 2011-2022 走看看