處理一串字串的關鍵字

餘默發表於2018-07-06
  • 一段文字有一個或都兩個關鍵字是比較特殊,字號或者顏色是有點區別,所以自己也嘗試擴充套件一個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複製程式碼


相關文章