.h
import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface GHAttributesLabel : UILabel
typedef void(^GHAttributesBlock)(NSRange poinRange);
/**
@param text 傳入富文本類型的字符串
@param actionText 要響應(yīng)事件的字符串
*/
- (void)setAttributesText: (NSMutableAttributedString *)text
actionText: (NSString *)actionText;
/**
點(diǎn)擊事件回調(diào)
*/
@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;
// 復(fù)制
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 = @"注:本工具測試結(jié)果為一周內(nèi)平均每天鹽的攝入量為____毫升刹泄;問題中涉及的食物均以一人份為準(zhǔn)余寥;本軟件更適用于成人;建議您及您的家人使用低鈉鹽____本軟件更適用于成人";
// 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:@"請(qǐng)輸入"];
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) {
//改變多次搜索時(shí)searchRange的位置
searchRange = NSMakeRange(NSMaxRange(range), [self length] - NSMaxRange(range));
//設(shè)置富文本
[mutableAttributedStr addAttribute:NSForegroundColorAttributeName value:color range:range];
}
}
return mutableAttributedStr;
}