美文网首页
iOS rangeOfString 搜索string重复的解决

iOS rangeOfString 搜索string重复的解决

作者: 风___________ | 来源:发表于2019-06-10 14:49 被阅读0次

NSString 加了一个延展

// 思路就是 搜到一个字符串之后,把这个字符串从主串中移除,然后累加删除的长度,还原成真正的NSRange
- (void)traverseSeletedArray:(NSArray<NSString *> *)selectedStringArray
               ByFinishBlock:(void(^)(NSRange selRange,NSInteger index))finishBlock{
    NSUInteger deleteLenth = 0;
    NSMutableString *rangeString = self.mutableCopy;
    for (NSUInteger index = 0; index < selectedStringArray.count; index++) {
        NSString *selectedString = selectedStringArray[index];
        NSRange searchRang = [rangeString rangeOfString:selectedString];
        [rangeString replaceCharactersInRange:searchRang withString:@""];
        NSRange selRange = NSMakeRange(deleteLenth+searchRang.location, searchRang.length);
        if (selRange.location != NSNotFound && finishBlock) finishBlock(selRange,index);
        deleteLenth += searchRang.length;
    }
}

下面是一个字符串类型转化的延展

.h

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
@interface NSString (CFTypeConversion)
//获取字符串的尺寸
- (CGSize)toStringSizeAndFountSize:(float)fountSize
                     AndFountWidth:(float)fountWidth
                        AndMaxSize:(CGSize)maxSize;

// 转化为url
- (NSURL *)filePathToURL;
// 时间格式的字符串转化为time
- (NSTimeInterval)toTimeInterval;
- (NSTimeInterval)toTimeIntervalByDateComponents:(NSString *)dateComponents;
#pragma mark 转化为富文本
/**
 颜色
 
 @param nolmalColor 颜色
 @param selectedStringArray 颜色
 @param selColor 颜色
 @return 富文本
 */
- (NSMutableAttributedString *)toAttributedStringAndNormalColor:(UIColor *)nolmalColor
                                               AndSeletedString:(NSArray<NSString *> *)selectedStringArray
                                               AndSelectenColor:(UIColor *)selColor;

- (NSMutableAttributedString *)toAttributedStringAndNormalColor:(UIColor *)nolmalColor
                                               AndSeletedString:(NSArray<NSString *> *)selectedStringArray
                                          AndSelectenColorArray:(NSArray<UIColor *> *)selColorArray;


/**
 字体
 
 @param nolmalSize 字体大小
 @param selectedStringArray 选中
 @param selSize 字体大小
 @return 富文本
 */
- (NSMutableAttributedString *)toAttributedStringAndNormalSize:(float)nolmalSize
                                              AndSeletedString:(NSArray<NSString *> *)selectedStringArray
                                              AndSelectenColor:(float )selSize;


/**
 文字和字体
 
 @param nolmalColor 颜色
 @param normalFont 字体
 @param selectedStringArray 选中
 @param selColor 颜色
 @param selectFont 字体
 @return 富文本
 */
- (NSMutableAttributedString *)toAttributedStringAndNormalColor:(UIColor *)nolmalColor
                                                  AndNormalFont:(UIFont *)normalFont
                                               AndSeletedString:(NSArray<NSString *> *)selectedStringArray
                                               AndSelectenColor:(UIColor *)selColor
                                                  AndSelectFont:(UIFont *)selectFont;
@end

.m


#import "NSString+CFTypeConversion.h"

@implementation NSString (CFTypeConversion)
- (CGSize)toStringSizeAndFountSize:(float)fountSize
                     AndFountWidth:(float)fountWidth
                        AndMaxSize:(CGSize)maxSize{
    UIFont *fount = [UIFont systemFontOfSize:fountSize weight:fountWidth];
    if (fountWidth == -1) {
        fount = [UIFont systemFontOfSize:fountSize];
    }
    NSDictionary *dic = @{NSFontAttributeName:fount};//输入的字体的大小
    NSStringDrawingOptions options = NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading;
    CGSize stringSize = [self boundingRectWithSize:maxSize options:options attributes:dic context:nil].size;
    return stringSize;
}
- (NSURL *)filePathToURL{
    NSURL *url = nil;
    if ([self containsString:@"http"]) {
        url = [NSURL URLWithString:self];
        return url;
    }
    url = [NSURL fileURLWithPath:self];
    return url;
}
- (NSTimeInterval)toTimeInterval{
    return [self toTimeIntervalByDateComponents:@"yyyy.MM.dd  HH:mm"];
}

- (NSTimeInterval)toTimeIntervalByDateComponents:(NSString *)dateComponents{
    NSDateFormatter *dateformat = [NSString createdDateFormatterByDateComponents:dateComponents];
    NSDate *date = [dateformat dateFromString:self];
    return date.timeIntervalSince1970;
}
+ (NSDateFormatter *)createdDateFormatterByDateComponents:(NSString *)dateComponents{
    NSDateFormatter *dateformat = [[NSDateFormatter alloc]init];
    [dateformat setDateStyle:NSDateFormatterNoStyle];
    [dateformat setTimeStyle:NSDateFormatterNoStyle];
    [dateformat setDateFormat:dateComponents];
    return dateformat;
}
// 富文本
- (NSMutableAttributedString *)toAttributedStringAndNormalColor:(UIColor *)nolmalColor
                                               AndSeletedString:(NSArray<NSString *> *)selectedStringArray
                                               AndSelectenColor:(UIColor *)selColor{
    NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc]initWithString:self];
    [attributeString setAttributes:@{NSForegroundColorAttributeName :nolmalColor
                                     }
                             range:NSMakeRange(0, self.length)];
    [self traverseSeletedArray:selectedStringArray ByFinishBlock:^(NSRange selRange,NSInteger index) {
        [attributeString setAttributes:@{NSForegroundColorAttributeName :selColor}
                                 range: selRange];
    }];
    return attributeString;
}

- (NSMutableAttributedString *)toAttributedStringAndNormalColor:(UIColor *)nolmalColor
                                               AndSeletedString:(NSArray<NSString *> *)selectedStringArray
                                          AndSelectenColorArray:(NSArray<UIColor *> *)selColorArray{
    NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc]initWithString:self];
    [attributeString setAttributes:@{NSForegroundColorAttributeName :nolmalColor
                                     }
                             range:NSMakeRange(0, self.length)];
    __weak NSArray<UIColor *> *weakArray = selColorArray;
    [self traverseSeletedArray:selectedStringArray ByFinishBlock:^(NSRange selRange,NSInteger index) {
        [attributeString setAttributes:@{NSForegroundColorAttributeName :weakArray[index]}
                                 range: selRange];
    }];
    // 调整行间距
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.lineSpacing = 3;
    NSRange range = NSMakeRange(0, [self length]);
    [attributeString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:range];
    return attributeString;
}


- (NSMutableAttributedString *)toAttributedStringAndNormalSize:(float)nolmalSize
                                              AndSeletedString:(NSArray<NSString *> *)selectedStringArray
                                              AndSelectenColor:(float )selSize{
    UIFont *normalFont = [UIFont systemFontOfSize:nolmalSize];
    UIFont *selectFont = [UIFont systemFontOfSize:selSize];
    NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc]initWithString:self];
    [attributeString addAttribute:NSFontAttributeName value:normalFont range:NSMakeRange(0, self.length)];
    [self traverseSeletedArray:selectedStringArray ByFinishBlock:^(NSRange selRange,NSInteger index) {
        [attributeString addAttribute:NSFontAttributeName value:selectFont range:selRange];
    }];
    return attributeString;
}

- (NSMutableAttributedString *)toAttributedStringAndNormalColor:(UIColor *)nolmalColor
                                                  AndNormalFont:(UIFont *)normalFont
                                               AndSeletedString:(NSArray<NSString *> *)selectedStringArray
                                               AndSelectenColor:(UIColor *)selColor
                                                  AndSelectFont:(UIFont *)selectFont{
    NSMutableAttributedString *attributeString = [self toAttributedStringAndNormalColor:nolmalColor AndSeletedString:selectedStringArray AndSelectenColor:selColor];
    [attributeString addAttribute:NSFontAttributeName value:normalFont range:NSMakeRange(0, self.length)];
    [self traverseSeletedArray:selectedStringArray ByFinishBlock:^(NSRange selRange,NSInteger index) {
        [attributeString addAttribute:NSFontAttributeName value:selectFont range:selRange];
    }];
    return attributeString;
}
#pragma mark 辅助方法
- (void)traverseSeletedArray:(NSArray<NSString *> *)selectedStringArray
               ByFinishBlock:(void(^)(NSRange selRange,NSInteger index))finishBlock{
    NSUInteger deleteLenth = 0;
    NSMutableString *rangeString = self.mutableCopy;
    for (NSUInteger index = 0; index < selectedStringArray.count; index++) {
        NSString *selectedString = selectedStringArray[index];
        NSRange searchRang = [rangeString rangeOfString:selectedString];
        [rangeString replaceCharactersInRange:searchRang withString:@""];
        NSRange selRange = NSMakeRange(deleteLenth+searchRang.location, searchRang.length);
        if (selRange.location != NSNotFound && finishBlock) finishBlock(selRange,index);
        deleteLenth += searchRang.length;
    }
}
@end

相关文章

网友评论

      本文标题:iOS rangeOfString 搜索string重复的解决

      本文链接:https://www.haomeiwen.com/subject/jkezxctx.html