文字實現點擊跳轉


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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM