iOS 条款-UITextView
2022-06-09 本文已影响0人
_Waiting_
.h
//
// HanAgreementView.h
//
//
// Created by Han on 2020/1/13.
// Copyright © 2020 Alibaba. All rights reserved.
//
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
typedef void (^HanAgreementBlock)(NSInteger index,NSString *url);
@class HanAgreementView;
@protocol HanAgreementViewDelegate <NSObject>
-(void)agreementView:(HanAgreementView *)view index:(NSInteger)index url:(NSString *)url;
@end
@class HanAgreementModel;
@interface HanAgreementView : UIView
@property(nonatomic,strong)UIFont *textFont;
@property(nonatomic,strong)UIColor *textColor;
@property(nonatomic, copy)HanAgreementBlock selectBlock;
@property (nonatomic,weak) id<HanAgreementViewDelegate> delegate;
-(instancetype)initWithText:(NSString *)text addModelArray:(NSArray<HanAgreementModel *> *)modelArray;
-(void)setLineSpacing:(CGFloat)height;
@end
@interface HanAgreementModel : NSObject
///要点击的文字
@property (nonatomic, copy) NSString *text;
///要点击文字的URL
@property (nonatomic, copy) NSString *url;
///要点击的文字 颜色 系统默认蓝色 无法修改??
@property (nonatomic, strong) UIColor *textColor;
///要点击的文字 字体
@property (nonatomic, strong) UIFont *textFont;
@end
NS_ASSUME_NONNULL_END
.m
//
// HanAgreementView.m
//
//
// Created by Han on 2020/1/13.
// Copyright © 2020 Alibaba. All rights reserved.
//
#import "HanAgreementView.h"
#import "UIColor+Han.h"
#import "HanBaseDefine.h"
@interface HanAgreementView ()<UITextViewDelegate>
@property(nonatomic,copy)NSString *text;
@property(nonatomic,copy)NSArray<HanAgreementModel *> *modelArray;
@property (nonatomic,strong)UITextView *textView;
@property (nonatomic,strong)NSMutableArray *itemArr;
@property (nonatomic,strong)NSMutableAttributedString *contentMString;
@end
@implementation HanAgreementView
-(instancetype)initWithText:(NSString *)text addModelArray:(nonnull NSArray<HanAgreementModel *> *)modelArray{
if (self = [super init]) {
self.contentMString = [[NSMutableAttributedString alloc] initWithString:text];
self.text = text;
self.modelArray = modelArray;
}
return self;
}
-(void)layoutSubviews{
[super layoutSubviews];
self.textView.frame = self.bounds;
}
-(BOOL)textView:(UITextView *)textView shouldInteractWithURL:(nonnull NSURL *)URL inRange:(NSRange)characterRange interaction:(UITextItemInteraction)interaction API_AVAILABLE(ios(10.0)){
NSString *url = [URL absoluteString];
if ([self.itemArr containsObject:url]) {
NSInteger index = [self.itemArr indexOfObject:url];
HanAgreementModel *mod = self.modelArray[index];
if (self.selectBlock) {
self.selectBlock(index,mod.url);
}
if (self.delegate && [self.delegate respondsToSelector:@selector(agreementView:index:url:)]) {
[self.delegate agreementView:self index:index url:mod.url];
}
return NO;
}
return YES;
}
-(void)setLineSpacing:(CGFloat)height{
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineSpacing = height; // 调整行间距
NSRange range = NSMakeRange(0, [self.text length]);
[self.contentMString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:range];
self.textView.attributedText = self.contentMString;
}
#pragma mark - setter
-(void)setTextFont:(UIFont *)textFont{
_textFont = textFont;
[self.contentMString addAttribute:NSFontAttributeName value:textFont range:NSMakeRange(0,self.contentMString.length)];
self.textView.attributedText = self.contentMString;
}
-(void)setTextColor:(UIColor *)textColor{
_textColor = textColor;
[self.contentMString addAttribute:NSForegroundColorAttributeName value:textColor range:NSMakeRange(0,self.text.length)];
self.textView.attributedText = self.contentMString;
}
-(void)setModelArray:(NSArray<HanAgreementModel *> *)modelArray{
_modelArray = modelArray;
NSInteger i = 0;
for (HanAgreementModel *mod in modelArray) {
NSRange range = [self.text rangeOfString:mod.text];
if (range.location != NSNotFound) {
if (mod.textFont) {
[self.contentMString addAttribute:NSFontAttributeName value:mod.textFont range:range];
}
if (mod.textColor) {
[self.contentMString addAttribute:NSForegroundColorAttributeName value:mod.textColor range:range];
}
[self.contentMString addAttribute:NSLinkAttributeName value:[NSString stringWithFormat:@"itemClick%ld",i] range:range];
[self.itemArr addObject:[NSString stringWithFormat:@"itemClick%ld",i]];
i++;
}
}
self.textView.attributedText = self.contentMString;
}
#pragma mark - get
- (NSMutableArray *)itemArr{
if (!_itemArr) {
_itemArr = [NSMutableArray arrayWithCapacity:5];
}
return _itemArr;
}
- (UITextView *)textView{
if (_textView == nil) {
_textView = [[UITextView alloc]initWithFrame:self.bounds];
_textView.delegate = self;
_textView.editable = NO;//必须禁止输入,否则点击将会弹出输入键盘
_textView.scrollEnabled = NO;//可选的,视具体情况而定
_textView.backgroundColor = [UIColor clearColor];
[self addSubview:_textView];
}
return _textView;
}
@end
@implementation HanAgreementModel
#pragma mark - get
-(NSString *)text{
if (_text == nil) {
_text = @"";
}
return _text;
}
-(NSString *)url{
if (_url == nil) {
_url = @"";
}
return _url;
}
@end
用法
HanAgreementModel *mod = [[HanAgreementModel alloc] init];
mod.text = @"「本服務條款及細則」";
mod.url = @"www.baidu.com1";
mod.textColor = HanHexColor(@"#F74D2E");
NSString *text = @"本人已閱讀並接受「本服務條款及細則」";
HanAgreementView *agreementView = [[HanAgreementView alloc] initWithText:text addModelArray:@[mod]];
[self.view addSubview:agreementView];
[agreementView mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerY.equalTo(agreeBtn.mas_centerY);
make.left.equalTo(agreeBtn.mas_right);
make.right.equalTo(self.view).offset(-20);;
make.height.mas_equalTo(30);
}];