iOS 注册协议和隐私协议自定义View

//textView 子类重写复制,粘贴和放大方法,禁止复制,粘贴和放大

#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

@interface WYPCusToomTextView : UITextView

@end

NS_ASSUME_NONNULL_END



#import "WYPCusToomTextView.h"

@implementation WYPCusToomTextView

- (BOOL)canBecomeFirstResponder{
    return NO;
    
}

@end

//自定义个TextView子类的背景视图

#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

typedef void(^ReginsterProtoocolAction)(void);//注册协议点酒事件
typedef void(^PrivityProtoocolAction)(void);//隐私政策点击事件

@interface WYPProtocolSupView : UIView

@property (nonatomic,strong) ReginsterProtoocolAction registerActionBlock;
@property (nonatomic,strong) PrivityProtoocolAction   privityActionBlock;

- (instancetype)initWithFrame:(CGRect)frame;

@end

NS_ASSUME_NONNULL_END



#import "WYPProtocolSupView.h"
#import "WYPCusToomTextView.h"

@interface WYPProtocolSupView()<UITextViewDelegate>

@end
@implementation WYPProtocolSupView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    
    if (self) {
        
        [self setUI];
    }
    return self;
}

-(void)setUI{
    
    //    子类重写禁止复制粘贴放大的方方
        WYPCusToomTextView *tvAgree = [[WYPCusToomTextView alloc]initWithFrame:self.bounds];
        tvAgree.userInteractionEnabled = YES;
        tvAgree.font = [UIFont systemFontOfSize:12];
        tvAgree.textColor = [UIColor yellowColor];
        tvAgree.editable = NO;//必须禁止输入,否则点击将弹出输入键盘
        tvAgree.scrollEnabled = NO;
        tvAgree.delegate = self;
        tvAgree.textContainerInset = UIEdgeInsetsMake(0,0, 0, 0);
        [self addSubview:tvAgree];
        
        NSString *agreeStr = [NSString stringWithFormat:@"%@《%@》与《%@》",@"我已阅读并同意",@"隐私政策",@"用户协议"];/// @"我已阅读并同意《隐私政策》《用户协议》";
        NSMutableAttributedString *diffString = [[NSMutableAttributedString alloc] initWithString:agreeStr];
    //    默认字体颜色设置(、不设置默认是黑色的) tvAgree.textColor = [UIColor yellowColor];这个设置不起作用
        [diffString addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:[[diffString string] rangeOfString:@"我已阅读并同意"]];/// @"我已阅读并同意"
        [diffString addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor]  range:[[diffString string] rangeOfString:@"与"]];
    //    设置点击的文字的链接标记
        [diffString addAttribute:NSLinkAttributeName
                               value:@"privacy://"
                               range:[[diffString string] rangeOfString:[NSString stringWithFormat:@"《%@》",@"隐私政策"]]];/// 《隐私政策》
        [diffString addAttribute:NSLinkAttributeName
                           value:@"delegate://"
                           range:[[diffString string] rangeOfString:[NSString stringWithFormat:@"《%@》",@"用户协议"]]];/// 《用户协议》
    //    设置点击文字的颜色
        tvAgree.linkTextAttributes = @{NSForegroundColorAttributeName: [UIColor redColor]};
        
        tvAgree.attributedText = diffString;

}
#pragma mark - UITextViewDelegate
/// 隐私,协议
-(BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange interaction:(UITextItemInteraction)interaction{
    /// 隐私
    if ([[URL scheme] isEqualToString:@"privacy"]){
        
        if (self.registerActionBlock) {
            self.registerActionBlock();
        }
        /// 跳转隐私政策界面
    }else if ([[URL scheme] isEqualToString:@"delegate"]) {
        /// 跳转用户协议界面
        
        if (self.privityActionBlock) {
            self.privityActionBlock();
        }
    }
    return NO;//返回No是长按链接文字不会弹出分享等弹框
}

@end

//在控制器里面调用:

    WYPProtocolSupView *tvAgree = [[WYPProtocolSupView alloc]initWithFrame:CGRectMake(50, 300, self.view.bounds.size.width - 100, 100)];
    tvAgree.registerActionBlock = ^{
        NSLog(@"registerActionBlockregisterActionBlockregisterActionBlock");
    };
    tvAgree.privityActionBlock = ^{
        NSLog(@"privityActionBlockprivityActionBlockprivityActionBlock");

    };
    [self.view addSubview:tvAgree];
    

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值