zoukankan      html  css  js  c++  java
  • 文字实现点击跳转

    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;
    }   
     
    
  • 相关阅读:
    【心情】一天又一天
    【转】深度学习目标检测的整体架构描述(one-stage/two-stage/multi-stage)
    如何转载CSDN以及博客园的文章
    【转】Faster RCNN原理分析(二):Region Proposal Networks详解
    Lintcode 627
    遇黑中介打官司拿到房租的成功案例
    记录音视频不同步的问题及解决过程
    ffmpeg 使用笔记
    centos min安装后使用gclient
    剑指 Offer 26. 树的子结构
  • 原文地址:https://www.cnblogs.com/lovemargin/p/12262281.html
Copyright © 2011-2022 走看看