PrivacyView.m 5.5 KB
//
//  PrivacyView.m
//  DreamSleep
//
//  Created by peter on 2022/4/6.
//

#import "PrivacyView.h"
#import "PrivacyViewController.h"
#import "LeadingController.h"

@interface PrivacyView () <UITextViewDelegate>
@property (nonatomic, strong) UIView *bigView;
@property (nonatomic, strong) UILabel *setLb;
@property (nonatomic, strong) UIButton *cancelBtn;
@property (nonatomic, strong) UIButton *confirmBtn;
@property (nonatomic, strong) UITextView *textView;
@end

@implementation PrivacyView

-(void)layoutSubviews {
    [super layoutSubviews];
    
    [self initView];
}

- (void)initView {
    self.bigView = [UIView new];
    [self addSubview:self.bigView];
    self.bigView.backgroundColor = [UIColor whiteColor];
    self.bigView.layer.cornerRadius = 24;
    self.bigView.layer.masksToBounds = YES;
    [self.bigView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.mas_centerX);
        make.centerY.equalTo(self.mas_centerY);
        make.width.equalTo(@280);
        make.height.equalTo(@236);
    }];
    
    self.setLb = [UILabel new];
    [self.bigView addSubview:self.setLb];
    self.setLb.text = @"隐私政策";
    self.setLb.textAlignment = NSTextAlignmentCenter;
    self.setLb.font = [UIFont systemFontOfSize:16 weight:UIFontWeightMedium];
    self.setLb.textColor = MainTextColor;
    [self.setLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.bigView.mas_top);
        make.left.equalTo(self.bigView.mas_left);
        make.right.equalTo(self.bigView.mas_right);
        make.height.equalTo(@40);
    }];
    
    self.cancelBtn = [UIButton new];
    [self.bigView addSubview:self.cancelBtn];
    [self.cancelBtn addTarget:self action:@selector(cancelBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.cancelBtn setTitle:@"不同意" forState:UIControlStateNormal];
    [self.cancelBtn setTitleColor:MainTextColor forState:UIControlStateNormal];
    self.cancelBtn.titleLabel.font = [UIFont systemFontOfSize:16];
    [self.cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.bigView.mas_left).offset(40);
        make.bottom.equalTo(self.bigView.mas_bottom).offset(-12);
        make.height.equalTo(@21);
        make.width.equalTo(@60);
    }];
    
    self.confirmBtn = [UIButton new];
    [self.bigView addSubview:self.confirmBtn];
    [self.confirmBtn addTarget:self action:@selector(confirmBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.confirmBtn setTitle:@"同意" forState:UIControlStateNormal];
    [self.confirmBtn setTitleColor:BrandColor forState:UIControlStateNormal];
    self.confirmBtn.titleLabel.font = [UIFont systemFontOfSize:16];
    [self.confirmBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.bigView.mas_right).offset(-56);
        make.bottom.equalTo(self.bigView.mas_bottom).offset(-12);
        make.height.equalTo(@21);
        make.width.equalTo(@50);
    }];
    
    self.textView = [UITextView new];
    self.textView.delegate = self;
    self.textView.editable = NO;
    [self.bigView addSubview:self.textView];
    
    // UITextView换行
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:PrivacyStatement attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15]}];
    // 设置行间距和段落间距
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle setLineSpacing:5];
    [paragraphStyle setParagraphSpacing:5];
    [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, PrivacyStatement.length)];
    // 设置超链接文本样式
    // MARK: 若NSLinkAttributeName字段包含中文时,需要对其进行特殊处理,否则无法触发UITextViewDelegate
    [attributedString addAttributes:@{NSForegroundColorAttributeName:BrandColor, NSLinkAttributeName:UserServiceAgreement} range:[PrivacyStatement rangeOfString:@"用户服务协议"]];
    [attributedString addAttributes:@{NSForegroundColorAttributeName:BrandColor, NSLinkAttributeName:PrivacyPolicy} range:[PrivacyStatement rangeOfString:@"隐私政策"]];
    self.textView.linkTextAttributes = @{NSForegroundColorAttributeName:BrandColor};
    self.textView.attributedText = attributedString;
    self.textView.font = [UIFont systemFontOfSize:15];
    self.textView.textColor = ColorFromRGB(145, 145, 145);
    [self.textView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.setLb.mas_bottom);
        make.left.equalTo(self.bigView.mas_left).offset(5);
        make.right.equalTo(self.bigView.mas_right).offset(-5);
        make.bottom.equalTo(self.confirmBtn.mas_top).offset(-10);
    }];
}

- (void)cancelBtnClick:(UIButton *)sender {
    DSLog(@"%s", __func__);
}

- (void)confirmBtnClick:(UIButton *)sender {
    DSLog(@"%s", __func__);
    // 本地存储隐私政策标识
    
    // 切换到引导页
    DSKeyWindow.rootViewController = [[LeadingController alloc] init];
}

#pragma mark - UITextViewDelegate
- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange interaction:(UITextItemInteraction)interaction {
    NSString *title = [URL.absoluteString isEqualToString:UserServiceAgreement] ? @"用户服务协议" : @"隐私政策";
    [self.ds_viewController.navigationController pushViewController:[[PrivacyViewController alloc] initWithTitle:title link:URL isDetail:YES] animated:YES];
    return NO;
}

@end