码迷,mamicode.com
首页 > 其他好文 > 详细

文字实现点击跳转

时间:2020-02-05 10:12:16      阅读:144      评论:0      收藏:0      [点我收藏+]

标签:range   pod   click   oat   instr   ring   nsrange   demo   coap   

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

文字实现点击跳转

标签:range   pod   click   oat   instr   ring   nsrange   demo   coap   

原文地址:https://www.cnblogs.com/lovemargin/p/12262281.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!