首页 > 代码库 > 处理一串字符串的关键字

处理一串字符串的关键字

<style>p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px "PingFang SC" } p.p2 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px "PingFang SC"; min-height: 25.0px } p.p3 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #cf8724 } p.p4 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #e82300 } p.p5 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #000000; min-height: 13.0px } p.p6 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #000000 } p.p7 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #cf8724 } p.p8 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px "PingFang SC"; color: #cf8724 } p.p9 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #36568a } p.p10 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #36568a; min-height: 13.0px } p.p11 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #c35900 } p.p12 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #587ea8 } p.p13 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px "PingFang SC"; color: #36568a } span.s1 { } span.s2 { color: #777897 } span.s3 { color: #36568a } span.s4 { font: 11.0px "PingFang SC" } span.s5 { color: #c35900 } span.s6 { font: 11.0px Menlo } span.s7 { color: #587ea8 } span.s8 { color: #e82300 } span.s9 { color: #000000 } span.s10 { font: 11.0px Menlo; color: #000000 }</style>

最近做项目遇到一种情况,就是一段文字有一个或都两个关键字是比较特殊,字号或者颜色是有点区别,所以自己也尝试扩展一个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

 

 

 

处理一串字符串的关键字