zoukankan      html  css  js  c++  java
  • 处理一串字符串的关键字

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

     

     

     

  • 相关阅读:
    dwz tabs table实现翻页及各tabs查询
    DruidDataSource配置
    利用blob对象实现大文件分片上传
    HTML5 File API 全介绍
    JS获取当前网页内容,创建文件并下载,URL.createObjectURL和URL.revokeObjectURL
    使用 CSS 接收用户的点击事情并对相关节点进行操作
    Flex布局
    background: inherit制作倒影、单行居中两行居左超过两行省略
    层叠顺序与堆栈上下文、font-family字体定义顺序的
    简单使用GA监控网站浏览行为
  • 原文地址:https://www.cnblogs.com/ljj-Andrew-519/p/7169265.html
Copyright © 2011-2022 走看看