iOS富文本Label实现点击事件,类似Word在横线上输入编辑

2022-05-19  本文已影响0人  贺乾龙

.h

import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

@interface GHAttributesLabel : UILabel
typedef void(^GHAttributesBlock)(NSRange poinRange);
/**

@param text 传入富文本类型的字符串
@param 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

@end

@interface GHAttributesLabel()<UITextViewDelegate>

@property (nonatomic , strong) GHTextView *textView ;

@property (nonatomic , copy) NSString *actionText ;

/** <#注释#>*/
@property (nonatomic, assign) NSRange range;

@end
@implementation GHAttributesLabel

// return YES;
}

@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];

}

}
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;

}

上一篇 下一篇

猜你喜欢

热点阅读