文字实现点击跳转

cocoapods 导入三方库

  pod 'YBAttributeTextTapAction'

头文件添加

#import "UILabel+YBAttributeTextTapAction.h"

demo代码如下:

NSString * showText = @"秦始皇是出生于赵国都城邯郸(今邯郸),并在此度过了少年时期。前247年,13岁时即王位。 前238年,22岁时,在故都雍城举行了国君成人加冕仪式,开始“亲理朝政”,除掉吕不韦、嫪毐等人, 重用李斯、尉缭,自前230年至前221年,先后灭韩、赵、魏、楚、燕、齐六国,wo39岁时完成了统一中国大业,建立起一个以汉族为主体统一的中央集权的强大国家——秦朝,并奠定中国本土的疆域。";
    UILabel *comment = [UILabel new];
    comment.textColor = [UIColor redColor];
    comment.numberOfLines = 0;
    comment.attributedText = [self getAttributeWith:@[@"邯郸"] string:showText orginFont:15 orginColor:[UIColor darkGrayColor] attributeFont:18 attributeColor:[UIColor blueColor]];
    [self.view addSubview:comment];
    [comment mas_makeConstraints:^(MASConstraintMaker *make) {
           make.top.equalTo(@200);
        make.left.equalTo(@50);
        make.right.equalTo(@-50);
       }];
    [comment yb_addAttributeTapActionWithStrings:@[@"wo",@"邯郸",@"邯郸"] tapClicked:^(UILabel *label, NSString *string, NSRange range, NSInteger index) {
        NSLog(@"%@",string);
    }];
 
 #pragma mark --- 富文本设置
- (NSAttributedString *)getAttributeWith:(id)sender
                                  string:(NSString *)string
                               orginFont:(CGFloat)orginFont
                              orginColor:(UIColor *)orginColor
                           attributeFont:(CGFloat)attributeFont
                          attributeColor:(UIColor *)attributeColor
{
    __block  NSMutableAttributedString *totalStr = [[NSMutableAttributedString alloc] initWithString:string];
    [totalStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:orginFont] range:NSMakeRange(0, string.length)];
    [totalStr addAttribute:NSForegroundColorAttributeName value:orginColor range:NSMakeRange(0, string.length)];
    
    if ([sender isKindOfClass:[NSArray class]]) {
        
        __block NSString *oringinStr = string;
        __weak typeof(self) weakSelf = self;
        
        [sender enumerateObjectsUsingBlock:^(NSString *  _Nonnull str, NSUInteger idx, BOOL * _Nonnull stop) {
            
            NSRange range = [oringinStr rangeOfString:str];
            [totalStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:attributeFont] range:range];
            [totalStr addAttribute:NSForegroundColorAttributeName value:attributeColor range:range];
            oringinStr = [oringinStr stringByReplacingCharactersInRange:range withString:[weakSelf getStringWithRange:range]];
        }];
        
    }else if ([sender isKindOfClass:[NSString class]]) {
        
        NSRange range = [string rangeOfString:sender];
        
        [totalStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:attributeFont] range:range];
        [totalStr addAttribute:NSForegroundColorAttributeName value:attributeColor range:range];
    }
    return totalStr;
}

#pragma mark --- 通过range获取字符串
- (NSString *)getStringWithRange:(NSRange)range
{
    NSMutableString *string = [NSMutableString string];
    for (int i = 0; i < range.length ; i++) {
        [string appendString:@" "];
    }
    return string;
}   
 
posted @   Love margin  阅读(1077)  评论(0编辑  收藏  举报
编辑推荐:
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
阅读排行:
· 终于写完轮子一部分:tcp代理 了,记录一下
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
点击右上角即可分享
微信分享提示