//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];