美文网首页
iOS OC字符串处理

iOS OC字符串处理

作者: 邓布利多教授 | 来源:发表于2022-04-06 09:37 被阅读0次

创建NSAttributedString扩展

.h文件

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

/// 字符串设置不同颜色和字体大小
/// @param allString 整个字符串
/// @param strings 需要修改样式的子字符串数组
/// @param colors 颜色数组
/// @param fonts 字体数组
/// @param lineSpacing 行间距
+ (NSAttributedString *)stringWithAllString:(NSString *)allString
                                 subStrings:(NSArray<NSString *> * _Nullable)strings
                                     colors:(NSArray<UIColor *> * _Nullable)colors
                                      fonts:(NSArray<UIFont *> * _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<NSString *> *)strings
                                     colors:(NSArray<UIColor *> *)colors
                                      fonts:(NSArray<UIFont *> *)fonts
                                   aligment:(NSTextAlignment)aligment {
    return [self stringWithAllString:allString subStrings:strings colors:colors fonts:fonts lineSpacing:0 aligment:aligment];
}

/// 字符串设置不同颜色和字体大小 - 可设置行间距
+ (NSAttributedString *)stringWithAllString:(NSString *)allString
                                 subStrings:(NSArray<NSString *> *)strings
                                     colors:(NSArray<UIColor *> *)colors
                                      fonts:(NSArray<UIFont *> *)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字符串处理

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