.h
import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface GHAttributesLabel : UILabel
typedef void(^GHAttributesBlock)(NSRange poinRange);
/**
@param text 传入富文本类型的字符串
@param actionText 要响应事件的字符串
*/
- (void)setAttributesText: (NSMutableAttributedString *)text
actionText: (NSString *)actionText;
/**
点击事件回调
*/
@property (nonatomic , copy) GHAttributesBlock actionBlock;
.m
//
// GHAttributesLabel.m
// GHAttributesLabelDemo
//
// Created by zhaozhiwei on 2019/1/20.
// Copyright © 2019年 GHome. All rights reserved.
//
import "GHAttributesLabel.h"
@interface GHTextView : UITextView
@end
@implementation GHTextView
-
(BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
// 返回NO为禁用,YES为开启
// 粘贴
if (action == @selector(paste:)) return NO;
// 剪切
if (action == @selector(cut:)) return NO;
// 复制
if (action == @selector(copy:)) return NO;
// 选择
if (action == @selector(select:)) return NO;
// 选中全部
if (action == @selector(selectAll:)) return NO;
// 删除
if (action == @selector(delete:)) return NO;
// 分享
if (action == @selector(share)) return NO;
return [super canPerformAction:action withSender:sender];
} -
(BOOL)canBecomeFirstResponder {
return NO;
}
@end
@interface GHAttributesLabel()<UITextViewDelegate>
@property (nonatomic , strong) GHTextView *textView ;
@property (nonatomic , copy) NSString *actionText ;
/** <#注释#>*/
@property (nonatomic, assign) NSRange range;
@end
@implementation GHAttributesLabel
- (void)setAttributesText: (NSMutableAttributedString *)text actionText: (NSString *)actionText{
self.textView.attributedText = text;
self.actionText = actionText;
// self.range = actionRange;
}
- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange interaction:(UITextItemInteraction)interaction {
// if ([textView.text containsString:self.actionText]) {
if (self.actionBlock) {
self.actionBlock(characterRange);
}
return NO;
// }
// return YES;
}
-
(instancetype)init {
if (self == [super init]) {
[self setupUI];
[self configuration];
}
return self;
} -
(instancetype)initWithFrame:(CGRect)frame {
if (self == [super initWithFrame:frame]) {
[self setupUI];
[self configuration];
}
return self;
} -
(void)configuration {
self.userInteractionEnabled = YES;
} -
(void)setupUI {
[self addSubview:self.textView];
} -
(void)layoutSubviews {
self.textView.frame = self.bounds;
}
- (GHTextView *)textView {
if (_textView == nil) {
_textView = [[GHTextView alloc]init];
_textView.backgroundColor = self.backgroundColor;
_textView.textColor = self.textColor;
self.textColor = [UIColor clearColor];
_textView.font = self.font;
_textView.scrollEnabled = NO;
_textView.text = self.text;
_textView.delegate = self;
_textView.editable = NO;
_textView.textAlignment = self.textAlignment;
_textView.linkTextAttributes = @{NSForegroundColorAttributeName:[UIColor redColor]};
}
return _textView;
}
@end
使用
-(void)test{
GHAttributesLabel *attributesLabel = [[GHAttributesLabel alloc]initWithFrame:CGRectMake(10, 200, [UIScreen mainScreen].bounds.size.width - 20, 250)];
NSString *temp = @"注:本工具测试结果为一周内平均每天盐的摄入量为____毫升;问题中涉及的食物均以一人份为准;本软件更适用于成人;建议您及您的家人使用低钠盐____本软件更适用于成人";
// NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:temp];
NSString *actionStr = @"____";
NSMutableAttributedString *attrStr = [temp keyWords:actionStr withKeyWordsColor:[UIColor redColor]];
// NSRange range = [temp rangeOfString:actionStr];
// NSLog(@"range%@",NSStringFromRange(range));
NSArray *actionArr = [self rangeOfSubString:actionStr inString:temp];
NSLog(@"===:%@",[self rangeOfSubString:actionStr inString:temp]);
for (int i = 0; i < actionArr.count; i++) {
NSValue *value = actionArr[i];
NSRange actionRange = [value rangeValue];
[attrStr addAttribute:NSLinkAttributeName
value:actionStr
range: actionRange];
}
[attrStr addAttribute:NSFontAttributeName
value:[UIFont systemFontOfSize:20]
range:NSMakeRange(0, attrStr.length)];
__block GHAttributesLabel *weakLabel = attributesLabel;
NSMutableDictionary *titleDict = [NSMutableDictionary dictionary];
attributesLabel.actionBlock = ^(NSRange poinRange) {
NSLog(@"poinRange%@",NSStringFromRange(poinRange));
__block NSString *titleStr;
NSString *properTitle = [titleDict objectForKey:[NSString stringWithFormat:@"%lu",(unsigned long)poinRange.location]];
TextInputViewController *inputVC = [self inputVCWithProperty:properTitle NavName:@"请输入"];
inputVC.maxCount = 200;
inputVC.GetText = ^(NSString *title){
titleStr = title.length ? title : @"____";
[titleDict setValue:titleStr forKey:[NSString stringWithFormat:@"%lu",(unsigned long)poinRange.location]];
NSRange contentRange = {poinRange.location,[title length]};
[attrStr replaceCharactersInRange:poinRange withString:title];
[attrStr addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:contentRange];
weakLabel.attributedText = attrStr;
[weakLabel setAttributesText:attrStr actionText:actionStr];
};
};
[attributesLabel setAttributesText:attrStr actionText:actionStr];
[self.view addSubview:attributesLabel];
}
- (NSArray)rangeOfSubString:(NSString)subStr inString:(NSString*)string {
NSMutableArray *rangeArray = [NSMutableArray array];
NSString *string1 = [string stringByAppendingString:subStr];
NSString *temp;
for(int i =0; i < string.length; i ++) {
temp = [string1 substringWithRange:NSMakeRange(i, subStr.length)];
if ([temp isEqualToString:subStr]) {
NSRange range = {i,subStr.length};
[rangeArray addObject: [NSValue valueWithRange:range]];
}
}
return rangeArray;
}
-(NSMutableAttributedString *)keyWords:(NSString *)keyWords withKeyWordsColor:(UIColor *)color
{
NSMutableAttributedString *mutableAttributedStr = [[NSMutableAttributedString alloc] initWithString:self];
if (color == nil) {
color = [UIColor colorWithHexString:@"52c683"];
}
if (keyWords.length<=0) {
return mutableAttributedStr;
}
for (NSInteger j=0; j<=keyWords.length-1; j++) {
NSRange searchRange = NSMakeRange(0, [self length]);
NSRange range;
NSString *singleStr = [keyWords substringWithRange:NSMakeRange(j, 1)];
while
((range = [self rangeOfString:singleStr options:0 range:searchRange]).location != NSNotFound) {
//改变多次搜索时searchRange的位置
searchRange = NSMakeRange(NSMaxRange(range), [self length] - NSMaxRange(range));
//设置富文本
[mutableAttributedStr addAttribute:NSForegroundColorAttributeName value:color range:range];
}
}
return mutableAttributedStr;
}
网友评论