最近做了一个有关用户勾选《用户协议》和《隐私政策》的功能,最开始使用系统自带的UItextView的富文本功能,实现图文并排点击事件,但是点击高亮的文字和图片后,先会变灰,再进行响应事件。
折腾了半天没有解决,最后直接使用了YYLabel来实现这个功能。
效果图如下
效果图步骤一:导入YYText框架
步骤二:添加代码
//
// ViewController.m
// XXProtocol
//
// Created by Summer on 2018/6/12.
// Copyright © 2018年 Summer. All rights reserved.
//
#import "ViewController.h"
#import "YYText.h"
@interface ViewController ()
@property (nonatomic , strong) YYLabel *yyLabel;
@property (nonatomic , assign) BOOL isSelect;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
_yyLabel = [[YYLabel alloc]initWithFrame:CGRectMake(20, 100, self.view.bounds.size.width - 40, 40)];
_yyLabel.textVerticalAlignment = YYTextVerticalAlignmentCenter;
_yyLabel.textAlignment = NSTextAlignmentCenter;
[self.view addSubview:_yyLabel];
[self protocolIsSelect:NO];
}
- (void)protocolIsSelect:(BOOL)isSelect{
//设置整段字符串的颜色
UIColor *color = self.isSelect ? [UIColor blackColor] : [UIColor lightGrayColor];
NSDictionary *attributes = @{NSFontAttributeName:[UIFont fontWithName:@"PingFangSC-Regular" size:12], NSForegroundColorAttributeName: color};
NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:@" 注册即表示同意《用户协议》和《隐私政策》" attributes:attributes];
//设置高亮色和点击事件
[text yy_setTextHighlightRange:[[text string] rangeOfString:@"《用户协议》"] color:[UIColor orangeColor] backgroundColor:[UIColor clearColor] tapAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
NSLog(@"点击了《用户协议》");
}];
//设置高亮色和点击事件
[text yy_setTextHighlightRange:[[text string] rangeOfString:@"《隐私政策》"] color:[UIColor orangeColor] backgroundColor:[UIColor clearColor] tapAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
NSLog(@"点击了《隐私政策》");
}];
//添加图片
UIImage *image = [UIImage imageNamed:self.isSelect == NO ? @"unSelectIcon" : @"selectIcon"];
NSMutableAttributedString *attachment = [NSMutableAttributedString yy_attachmentStringWithContent:image contentMode:UIViewContentModeCenter attachmentSize:CGSizeMake(12, 12) alignToFont:[UIFont fontWithName:@"PingFangSC-Regular" size:12] alignment:(YYTextVerticalAlignment)YYTextVerticalAlignmentCenter];
//将图片放在最前面
[text insertAttributedString:attachment atIndex:0];
//添加图片的点击事件
[text yy_setTextHighlightRange:[[text string] rangeOfString:[attachment string]] color:[UIColor clearColor] backgroundColor:[UIColor clearColor] tapAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
__weak typeof(self) weakSelf = self;
weakSelf.isSelect = !weakSelf.isSelect;
[weakSelf protocolIsSelect:self.isSelect];
}];
_yyLabel.attributedText = text;
//居中显示一定要放在这里,放在viewDidLoad不起作用
_yyLabel.textAlignment = NSTextAlignmentCenter;
}
@end
对你有用,就请给我一个赞吧✌️✌️
有错之处,还请指出,感谢🙏🙏
网友评论