最近做项目遇到一种情况,就是一段文字有一个或都两个关键字是比较特殊,字号或者颜色是有点区别,所以自己也尝试扩展一个NSString类,下面的代码主要是针对项目而言:

 

 

具体展示:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

//  NSString+Extension.h

#import <Foundation/Foundation.h>

#import <UIKit/UIKit.h>

 

@interface NSString (Extension)

 

/** 两个string同种颜色不同字号 */

+ (NSMutableAttributedString *)ym_setAttribute:(NSString *)firstStr secondString:(NSString *)secondStr formerFont:(UIFont *)firstFont latterFont:(UIFont *)secondFont color:(UIColor *)color;

 

/** 两个string不同颜色不同字号 */

+ (NSMutableAttributedString *)ym_setattributes:(NSString *)firstStr secondString:(NSString *)secondStr formerFont:(UIFont *)firstFont latterFont:(UIFont *)secondFont firstColor:(UIColor *)firstColor secondColor:(UIColor *)secondColor;

 

/** 两个关键字同颜色同字号 */

+ (NSMutableAttributedString *)ym_setAttributes:(NSString *)normalStr normalFont:(UIFont *)normalFont normalColor:(UIColor *)normalColor firstSpecialStr:(NSString *)firstSpecialStr : (NSString *)secondSpecialStr specialFont:(UIFont *)specialFont specialColor:(UIColor *)specialColor;

 

/** 两个关键字不同颜色不字号 */

+ (NSMutableAttributedString *)ym_setAttributes:(NSString *)normalStr normalFont:(UIFont *)normalFont normalColor:(UIColor *)normalColor firstSpecialStr:(NSString *)firstSpecialStr : (NSString *)secondSpecialStr firstSpecialFont:(UIFont *)firstSpecialFont firstSpecialColor:(UIColor *)firstSpecialColor secondSpecialFont:(UIFont *)secondSpecialFont secondSpecialColor:(UIColor *)secondSpecialColor;

 

 

@end

 

 

//  NSString+Extension.m

 

#import "NSString+Extension.h"

 

@implementation NSString (Extension)

 

/** 两个string同种颜色不同字号 */

+ (NSMutableAttributedString *)ym_setAttribute:(NSString *)firstStr secondString:(NSString *)secondStr formerFont:(UIFont *)firstFont latterFont:(UIFont *)secondFont color:(UIColor *)color

{

    NSString *string = [NSString stringWithFormat:@"%@%@",firstStr,secondStr];

    NSMutableAttributedString *attrrSting = [[NSMutableAttributedString alloc] initWithString:string];

    

    NSUInteger firstLength = [firstStr length];

    NSUInteger secondLength = [secondStr length];

    

    [attrrSting addAttribute:NSFontAttributeName value:firstFont range:NSMakeRange(0, firstLength)];

    [attrrSting addAttribute:NSFontAttributeName value:secondFont range:NSMakeRange(firstLength, secondLength)];

    [attrrSting addAttribute:NSForegroundColorAttributeName value:color range:NSMakeRange(0, firstLength + secondLength)];

    

    return attrrSting;

}

 

/** 两个string不同颜色不同字号 */

+ (NSMutableAttributedString *)ym_setattributes:(NSString *)firstStr secondString:(NSString *)secondStr formerFont:(UIFont *)firstFont latterFont:(UIFont *)secondFont firstColor:(UIColor *)firstColor secondColor:(UIColor *)secondColor

{

    NSString *string = [NSString stringWithFormat:@"%@%@",firstStr,secondStr];

    NSMutableAttributedString *attrSting = [[NSMutableAttributedString alloc] initWithString:string];

    

    NSUInteger firstLength = [firstStr length];

    NSUInteger secondLength = [secondStr length];

    

    [attrSting addAttribute:NSFontAttributeName value:firstFont range:NSMakeRange(0, firstLength)];

    [attrSting addAttribute:NSFontAttributeName value:secondFont range:NSMakeRange(firstLength, secondLength)];

    [attrSting addAttribute:NSForegroundColorAttributeName value:firstColor range:NSMakeRange(0, firstLength)];

    [attrSting addAttribute:NSForegroundColorAttributeName value:secondColor range:NSMakeRange(firstLength, secondLength)];

    

    return attrSting;

}

 

/** 两个关键字同颜色同字号 */

+ (NSMutableAttributedString *)ym_setAttributes:(NSString *)normalStr normalFont:(UIFont *)normalFont normalColor:(UIColor *)normalColor firstSpecialStr:(NSString *)firstSpecialStr :(NSString *)secondSpecialStr specialFont:(UIFont *)specialFont specialColor:(UIColor *)specialColor

{

    NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:normalStr];

    NSRange oneRange = [normalStr rangeOfString:firstSpecialStr];

    NSRange twoRange = [normalStr rangeOfString:secondSpecialStr];

    //整个字符串的颜色和字体

    [attrString addAttribute:NSFontAttributeName value:normalFont range:NSMakeRange(0, normalStr.length)];

    [attrString addAttribute:NSForegroundColorAttributeName value:normalColor range:NSMakeRange(0, normalStr.length)];

    //第一个特殊字符串的颜色和字体

    [attrString addAttribute:NSFontAttributeName value:specialFont range:oneRange];

    [attrString addAttribute:NSForegroundColorAttributeName value:specialColor range:oneRange];

    //第二个特殊字符串的颜色和字体

    [attrString addAttribute:NSFontAttributeName value:specialFont range:twoRange];

    [attrString addAttribute:NSForegroundColorAttributeName value:specialColor range:twoRange];

    

    return attrString;

    

}

 

/** 两个关键字不同颜色不字号 */

+ (NSMutableAttributedString *)ym_setAttributes:(NSString *)normalStr normalFont:(UIFont *)normalFont normalColor:(UIColor *)normalColor firstSpecialStr:(NSString *)firstSpecialStr :(NSString *)secondSpecialStr firstSpecialFont:(UIFont *)firstSpecialFont firstSpecialColor:(UIColor *)firstSpecialColor secondSpecialFont:(UIFont *)secondSpecialFont secondSpecialColor:(UIColor *)secondSpecialColor

{

    NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] initWithString:normalStr];

    NSRange oneRange = [normalStr rangeOfString:firstSpecialStr];

    NSRange twoRange = [normalStr rangeOfString:secondSpecialStr];

    //整个字符串的颜色和字体

    [attr addAttribute:NSFontAttributeName value:normalFont range:NSMakeRange(0, normalStr.length)];

    [attr addAttribute:NSForegroundColorAttributeName value:normalColor range:NSMakeRange(0, normalStr.length)];

    //第一个特殊字符串的颜色和字体

    [attr addAttribute:NSFontAttributeName value:firstSpecialFont range:oneRange];

    [attr addAttribute:NSForegroundColorAttributeName value:firstSpecialColor range:oneRange];

    //第二个特殊字符串的颜色和字体

    [attr addAttribute:NSFontAttributeName value:secondSpecialFont range:twoRange];

    [attr addAttribute:NSForegroundColorAttributeName value:secondSpecialColor range:twoRange];

    

    return attr;

}

 

 

@end