zoukankan      html  css  js  c++  java
  • iOS富文本-NSAttributedString简单封装

    直接调用系统的写起来比较麻烦,封装一下

    因为要简单所以就写类方法

    WJAttributeStyle 基类

    #import <Foundation/Foundation.h>
    #import <UIKit/UIKit.h>
    /**
     *  基类富文本
     
    */
    @interface WJAttributeStyle : NSObject

    @property (nonatomic,strong)NSString *attributeName;
    @property (nonatomic,strong)id value;
    @property (nonatomic,assign)NSRange range;

    + (WJAttributeStyle *)attributeName:(NSString *)attributeName value:(id)value range:(NSRange)range;

    @end
    #import "WJAttributeStyle.h"

    @implementation WJAttributeStyle

    + (WJAttributeStyle *)attributeName:(NSString *)attributeName value:(id)value range:(NSRange)range {
        WJAttributeStyle *style = [[self classnew];
        style.attributeName = attributeName;
        style.value = value;
        style.range = range;
        return style;
    }

    @end

     衍生,继承于上面的基类封装颜色,和大小之后写起来会更简单

    颜色:

    #import "WJAttributeStyle.h"
    #import "WJForeGroundColorStyle.h"

    /**
     *  颜色富文本
     
    */
    @interface WJForeGroundColorStyle : WJAttributeStyle

    + (WJForeGroundColorStyle *)withColor:(UIColor *)color range:(NSRange)range;

    @end
    #import "WJForeGroundColorStyle.h"

    @implementation WJForeGroundColorStyle

    + (WJForeGroundColorStyle *)withColor:(UIColor *)color range:(NSRange)range {
        WJForeGroundColorStyle *style =
        (WJForeGroundColorStyle *)[WJForeGroundColorStyle attributeName:NSForegroundColorAttributeName value:color range:range];
        return style;
    }

    @end

     大小:

    #import "WJAttributeStyle.h"
    /**
     *  大小富文本
     
    */
    @interface WJFontStyle : WJAttributeStyle

    + (WJFontStyle *)withFonte:(UIFont *)font range:(NSRange)range;

    @end
    #import "WJFontStyle.h"

    @implementation WJFontStyle

    + (WJFontStyle *)withFonte:(UIFont *)font range:(NSRange)range {
        WJFontStyle *style =
        (WJFontStyle *)[WJFontStyle attributeName:NSFontAttributeName value:font range:range];
        return style;
    }
    @end

     然后用个类目来给字符串设置属性文字

    #import <Foundation/Foundation.h>
    #import "WJAttributeStyle.h"
    #import "WJForeGroundColorStyle.h"
    #import "WJFontStyle.h"
    @interface NSString (WJAttributeStyle)

    /**
     *  根据styles数组创建出富文本
     *
     *  @param styles WJAttributeStyle对象构成的数组
     *
     *  @return 富文本
     
    */
    - (NSAttributedString *)createAttributeStringWithStyles:(NSArray *)styles;

    @end
    #import "NSString+WJAttributeStyle.h"

    @implementation NSString (WJAttributeStyle)

    - (NSAttributedString *)createAttributeStringWithStyles:(NSArray *)styles {
        if (self.length <= 0) {
            return nil;
        }
        NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc]initWithString:self];
        for (int i = 0; i < styles.count; i ++) {
            WJAttributeStyle *style = styles[i];
            [attributeString addAttribute:style.attributeName
                                    value:style.value
                                    range:style.range];
            
        }
        return attributeString;
    }

    @end

    使用:

        NSString *string = @"这是一个测试";
        _label.attributedText = [string createAttributeStringWithStyles:
                                 @[[WJAttributeStyle attributeName:NSForegroundColorAttributeName
                                                             value:[UIColor redColor]
                                                             range:NSMakeRange(01)],
                                   [WJForeGroundColorStyle withColor:[UIColor grayColor] range:NSMakeRange(11)],
                                   [WJFontStyle withFonte:[UIFont systemFontOfSize:30] range:NSMakeRange(21)]
                                   ]];

    扩展UIKit:https://github.com/YouXianMing/BookTextView

    开源富文本:https://github.com/nicolasgoutaland/GONMarkupParser

    图文混排:https://github.com/12207480/TYAttributedLabel

  • 相关阅读:
    eclipse经常卡死、反应慢、内存溢出的解决方案
    PAC4J 初探
    suse11离线安装nginx
    linux修改乱码的文件名
    CentOS修改服务器系统时间
    Unable to open nested entry '********.jar' 问题解决
    openssl req(生成证书请求和自建CA)
    CRT证书转JKS证书
    如何创建一个自签名的SSL证书(X509)
    Redis分布式锁的深度剖析
  • 原文地址:https://www.cnblogs.com/hxwj/p/4663088.html
Copyright © 2011-2022 走看看