美文网首页iOS学习开发
UITextView 中间显示点击事件,不显示全选 复制 粘贴

UITextView 中间显示点击事件,不显示全选 复制 粘贴

作者: 荔枝lizhi_iOS程序猿 | 来源:发表于2018-06-01 13:17 被阅读48次
    1.效果图
    效果图

    找了好多都没起作用,在这里记录下,也帮助 和我有同样需求的同学。

    2.代码示例

    < QKNoCopyTextView.h >

    #import <UIKit/UIKit.h>
    
    @interface QKNoCopyTextView : UITextView
    
    @end
    

    不显示 放大镜,全选,复制 粘贴。代码 < QKNoCopyTextView.m >

    #import "QKNoCopyTextView.h"
    
    @implementation QKNoCopyTextView
    
    -(BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{
        if ([gestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]] || [gestureRecognizer isKindOfClass:[UITapGestureRecognizer class]]) {
            return [super gestureRecognizerShouldBegin:gestureRecognizer];
        }
        
        if ([gestureRecognizer isKindOfClass:[UILongPressGestureRecognizer class]]) {
            UILongPressGestureRecognizer *longGesture =(UILongPressGestureRecognizer *) gestureRecognizer;
            if( longGesture.minimumPressDuration < 0.35){
                return [super gestureRecognizerShouldBegin:gestureRecognizer];
            }else{
                longGesture.enabled = false;
            }
        }
        
        return false;
    }
    @end
    
    3.使用

    A.viewcontroller 设置

    self.numTipTextView.editable = NO;
    self.numTipTextView.delegate = self;
    [self configNumTipTextViewWithMessage:@"亲,您当天快捷免密登录剩余0次,请点击忘记密码重置登录密码"];
    

    B.设置UI

    -(void)configNumTipTextViewWithMessage:(NSString *)messageStr{
        if ([messageStr length] < 1) {
            messageStr = @"";
        }
        // 设置属性
        NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
        // 设置行间距
        paragraphStyle.paragraphSpacing = 1; // 段落间距
        paragraphStyle.lineSpacing = 1;      // 行间距
        NSDictionary *attributes = @{
                                     NSForegroundColorAttributeName:[UIColor redColor],
                                     NSParagraphStyleAttributeName:paragraphStyle
                                     };
        NSMutableAttributedString * attrStr = [[NSMutableAttributedString alloc] initWithString:messageStr attributes:attributes];
        [attrStr addAttributes:@{
                                 NSLinkAttributeName:@"忘记密码"
                                 }
                         range:[messageStr rangeOfString:@"忘记密码"]];
        self.numTipTextView.linkTextAttributes = @{NSForegroundColorAttributeName:[UIColor blueColor]}; // 修改可点击文字的颜色
        self.numTipTextView.attributedText = attrStr;
    }
    

    delegate 实现

    -(BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange {
        NSString *messageStr = self.numTipTextView.attributedText.string;
        NSRange range = [messageStr rangeOfString:@"忘记密码"];
        if (characterRange.location == range.location) {
            // 做你想做的事
            NSLog(@"忘记密码");
            QKFindPwdController *findPwdController = [[QKFindPwdController alloc] init];
            
            [self.viewController.navigationController pushViewController:findPwdController animated:true];
        }
        return YES;
    }
    

    参考 https://stackoverflow.com/questions/36198299/uitextview-disable-selection-allow-links

    如您喜欢此文,请点赞!

    相关文章

      网友评论

        本文标题:UITextView 中间显示点击事件,不显示全选 复制 粘贴

        本文链接:https://www.haomeiwen.com/subject/yqihsftx.html