• 获取重复字符串的range,设置attributedText


    之前项目的需求是未读人员显示绿色,已读人员显示黑色,测试今天提bug说存在未读人员显示的黑色...这就尴尬了,我完全不知道为啥,经过打断点调试程序,终于找到问题:就是他只会获取到第一个想等字符的range,知道问题所在就简单了,下面是我的demo:

    //
    //  ViewController.m
    //  demo
    //
    //  Created by 思 彭 on 17/2/15.
    //  Copyright © 2017年 思 彭. All rights reserved.
    //
    
    #import "ViewController.h"
    
    @interface ViewController ()
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
        
        NSMutableArray *arr = [self getRangeStr:@"dndjnjajnjnjfnandjefnrjandrfnra" findText:@"a"];
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(100, 100, 100, 60)];
        [self.view addSubview:label];
        
        // 将label上的text中的a字符标志绿色,其余黑色
        label.attributedText =  [self redStringWithText:@"njfenfjenfeffee" WithArray:@[@"e",@"e", @"a", @"r"]];
    }
    
    - (NSMutableAttributedString *)redStringWithText:(NSString *)text WithArray:(NSArray *)array {
        NSDictionary *attribs = @{
                                  NSForegroundColorAttributeName:[UIColor blackColor],
                                  NSFontAttributeName:[UIFont systemFontOfSize:13]
                                  };
        NSMutableAttributedString *attributedText =
        [[NSMutableAttributedString alloc] initWithString:text
                                               attributes:attribs];
        for (int i = 0; i < array.count; i++) {
            //        NSRange redTextRange =[text rangeOfString:array[i]];
            NSMutableArray *rangeArray =  [self getRangeStr:text findText: array[i]];
            for (NSInteger i = 0; i < rangeArray.count; i++) {
                NSValue *value = rangeArray[i];
                [attributedText setAttributes:@{NSForegroundColorAttributeName:[UIColor greenColor],NSFontAttributeName:[UIFont systemFontOfSize:13]} range:[value rangeValue]];
            }
        }
        return attributedText;
    }
    
    // 获取重复字符的range
    
    - (NSMutableArray *)getRangeStr:(NSString *)text findText:(NSString *)findText
    {
        NSMutableArray *arrayRanges = [NSMutableArray arrayWithCapacity:20];
        if (findText == nil && [findText isEqualToString:@""]) {
            return nil;
        }
        NSRange rang = [text rangeOfString:findText];
        if (rang.location != NSNotFound && rang.length != 0) {
            [arrayRanges addObject:[NSValue valueWithRange:rang]];
            NSRange rang1 = {0,0};
            NSInteger location = 0;
            NSInteger length = 0;
            for (int i = 0;; i++)
            {
                if (0 == i) {
                    location = rang.location + rang.length;
                    length = text.length - rang.location - rang.length;
                    rang1 = NSMakeRange(location, length);
                }else
                {
                    location = rang1.location + rang1.length;
                    length = text.length - rang1.location - rang1.length;
                    rang1 = NSMakeRange(location, length);
                }
                rang1 = [text rangeOfString:findText options:NSCaseInsensitiveSearch range:rang1];
                if (rang1.location == NSNotFound && rang1.length == 0) {
                    break;
                }else
                    [arrayRanges addObject:[NSValue valueWithRange:rang1]];
            }
            return arrayRanges;
        }
        return nil;
    }
    
    
    @end

    很详细,记录一下!!

  • 相关阅读:
    mouseenter和mouseleave,mouseover和mouseout
    哈哈哈
    instanceof与typeof
    js事件传参
    浮动与清除问题
    简易富文本编辑
    js之prototype
    json序列化
    我对Defer and Promise的实现
    Ajax 完整教程
  • 原文地址:https://www.cnblogs.com/pengsi/p/6402322.html
Copyright © 2020-2023  润新知