iOS OC字符串处理

创建NSAttributedString扩展

.h文件

/// 字符串设置不同颜色和字体大小
/// @param allString 整个字符串
/// @param strings 需要修改样式的子字符串数组
/// @param colors 颜色数组
/// @param fonts 字体数组
+ (NSAttributedString *)stringWithAllString:(NSString *)allString
                                 subStrings:(NSArray * _Nullable)strings
                                     colors:(NSArray * _Nullable)colors
                                      fonts:(NSArray * _Nullable)fonts
                                   aligment:(NSTextAlignment)aligment;

/// 字符串设置不同颜色和字体大小
/// @param allString 整个字符串
/// @param strings 需要修改样式的子字符串数组
/// @param colors 颜色数组
/// @param fonts 字体数组
/// @param lineSpacing 行间距
+ (NSAttributedString *)stringWithAllString:(NSString *)allString
                                 subStrings:(NSArray * _Nullable)strings
                                     colors:(NSArray * _Nullable)colors
                                      fonts:(NSArray * _Nullable)fonts
                                lineSpacing:(CGFloat)lineSpacing
                                   aligment:(NSTextAlignment)aligment;

/// 字符串设置行间距
/// @param string 整个字符串
/// @param lineSpacing 行间距
+ (NSAttributedString *)stringWithString:(NSString *)string
                             lineSpacing:(CGFloat)lineSpacing
                                aligment:(NSTextAlignment)aligment;

.m文件

/// 字符串设置不同颜色和字体大小
+ (NSAttributedString *)stringWithAllString:(NSString *)allString
                                 subStrings:(NSArray *)strings
                                     colors:(NSArray *)colors
                                      fonts:(NSArray *)fonts
                                   aligment:(NSTextAlignment)aligment {
    return [self stringWithAllString:allString subStrings:strings colors:colors fonts:fonts lineSpacing:0 aligment:aligment];
}

/// 字符串设置不同颜色和字体大小 - 可设置行间距
+ (NSAttributedString *)stringWithAllString:(NSString *)allString
                                 subStrings:(NSArray *)strings
                                     colors:(NSArray *)colors
                                      fonts:(NSArray *)fonts
                                lineSpacing:(CGFloat)lineSpacing
                                   aligment:(NSTextAlignment)aligment {
    
    NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphStyle alloc] init];
    paraStyle.lineBreakMode = NSLineBreakByCharWrapping;
    paraStyle.alignment = aligment;
    paraStyle.lineSpacing = lineSpacing; //设置行间距
    
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:allString
                                                                                         attributes:@{NSParagraphStyleAttributeName:paraStyle,
                                                                                                      NSKernAttributeName:@0.0f}];
    
    if (strings.count != colors.count || strings.count != fonts.count) {
        NSAssert(NO, @"字符串数组、色值数组或字体数组的count不匹配");
        return attributedString;
    }
    
    for (int i = 0; i < strings.count; i ++) {
        
        /// 子字符串
        NSString *string = strings[i];
        NSRange range = [[attributedString string] rangeOfString:string];
        
        /// 颜色
        UIColor *color = colors[i];
        [attributedString addAttribute:NSForegroundColorAttributeName value:color range:range];
        
        /// 字体大小
        UIFont *font = fonts[i];
        [attributedString addAttribute:NSFontAttributeName value:font range:range];
        
    }
    
    return attributedString;
    
}

+ (NSAttributedString *)stringWithString:(NSString *)string
                             lineSpacing:(CGFloat)lineSpacing
                                aligment:(NSTextAlignment)aligment {
    
    NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphStyle alloc] init];
    paraStyle.lineBreakMode = NSLineBreakByCharWrapping;
    paraStyle.alignment = NSTextAlignmentLeft;
    paraStyle.lineSpacing = lineSpacing; //设置行间距
    
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:string
                                                                                         attributes:@{NSParagraphStyleAttributeName:paraStyle,
                                                                                                      NSKernAttributeName:@0.0f}];
    return attributedString;
    
}

你可能感兴趣的:(iOS OC字符串处理)